Unstoppable Domains

Order Sub-Categories under Parent

Spaceship Spaceship
Watch
Impact
11
How will i be able to order my sub-categories under my parent?

Right now they show up as:

-Parent 1
-Parent 2
-Parent 3
-Parent 4
---Sub-Categories of 1
---Sub-Categories of 2
---Sub-Categories of 3
---Sub-Categories of 4

My code is:

PHP:
    $cat.='<select size="1" name="category" size="36" maxlength="255">';
    $cat.='<OPTION VALUE="___" selected>Select A Category</OPTION>';
    $sql_query_cat = "SELECT * FROM categories ORDER BY parent ASC";
    //store the SQL query in the result variable
    $result_cat = mysql_query($sql_query_cat);
    if(mysql_num_rows($result_cat))
    {
    //output as long as there are still available fields
    while($row = mysql_fetch_array($result_cat))
    {
	 $parent = stripslashes($row[parent]);
	 $categ = $row[title];
     $categ = stripslashes($categ);
	 if($parent=='0'){
	$cat.='<option>-'.$categ.'</option>';
 }else{
 	$cat.='<option>---'.$categ.'</option>';
	}
    }
	}
    $cat.='</select>';
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
basic way:
table looks like:
category_id (int)
category_name (Varchar)
category_parent (int)

PHP:
$sql = "SELECT * from categories where category_parent=0"; // get all records with no parent
$rs = mysql_query($sql);
while($parents = mysql_fetch_array($rs)){
 echo "<option value=\"$parents[category_id]\">$parents[category_name]</option>\n"; //write out the parent name
$nsql = "SELECT * from categories where category_parent='" . $parents[category_id] . "'"; // create the sql looking for the children of this parent
$nrs = mysql_query($nsql);
while($children= mysql_fetch_array($nrs)){
 echo "<option value=\"$children[category_id]\">-- $children[category_name]</option>\n";
   }
}

Somethinglike that.
Might have typos a bit, hard to code in this little box ;p
Only good for 2 levels though

THere's also traversal method which is very cool but not so basic
 
Last edited:
0
•••
I Edited it to work with the way my table is setup:
PHP:
    $cat.='<select size="1" name="category" size="36" maxlength="255">';
    $cat.='<OPTION VALUE="___" selected>Select A Category</OPTION>';
 $sql = "SELECT * FROM categories WHERE parent=0";
$rs = mysql_query($sql);
while(mysql_fetch_array($rs)){
  $pname = stripslashes($row[name]);
  $pid = stripslashes($row[id]);

 $cat.='<option value="$pid">'.$pname.'</option>';
$nsql = "SELECT * FROM categories WHERE parent='".$pid."'";
$nrs = mysql_query($nsql);
while(mysql_fetch_array($nrs)){
 $cname = stripslashes($row[name]);
 $cid = stripslashes($row[id]);
 $cat.='<option value="$cid">'.$cname.'</option>';
   }
}  
$cat.='</select>';

but it shows nothing in the drop down box, and abouts about 5x the size it should be.
 
0
•••
PM me your site's url, i can't guess what the problem is
 
0
•••
0
•••
can u take a SS of the html source too right around where the drop down box is so i can see what the error message is.
 
0
•••
This is the whole code for it...

PHP:
<? include('admin_header.php'); ?>
<b>Add a Tutorial</b><br>
<hr noshade size="1">
<?php

include '../database.php';
    $cat.='<select size="1" name="category" size="36" maxlength="255">';
    $cat.='<OPTION VALUE="NA" selected>Select A Category</OPTION>';

 $sql = 'SELECT * FROM categories WHERE parent=0';
$rs = mysql_query($sql);
while(mysql_fetch_array($rs)){
  $pname = stripslashes($row[name]);
  $pid = stripslashes($row[id]);

 $cat.='<option value="'.$pid.'">'.$pname.'</option>';
$nsql = "SELECT * FROM categories WHERE parent='".$pid."'";
$nrs = mysql_query($nsql);
while(mysql_fetch_array($nrs)){
 $cname = stripslashes($row[name]);
 $cid = stripslashes($row[id]);
 $cat.='<option value="'.$cid.'">'.$cname.'</option>';
   }
}  
$cat.='</select>';


if($_GET['action'] == "add") {

   $title = $_POST[title];
   $url = $_POST[url];
   $category = $_POST[category];
   $image = $_POST[image];
   $validate = $_POST[validate];
   $description = $_POST[description];


if(!title){  

echo "The Title is a required feild";
exit(); 

}

if(!$url){

echo "URL is a required feild";
exit();
}

$sql = mysql_query("INSERT INTO tutorials (id, title, url, category, image, validate, description)
       VALUES('NULL', '$title','$url','$category','$image','$validate','$description')") or die (mysql_error()); 
      
  echo "Tutorial Added. Redirecting...";
         echo "<meta http-equiv=Refresh content=3;url=?x=check>";
exit();

} else {
?>
<form name="message" method='post' action='?x=addtutorial&action=add'>
     <b>Title:</b><BR> 
       <input name='title' size='36' maxlength='255'><br><BR>
     <b>URL:<br></b>
       <input name='url' value='http://' size='36' maxlength='255'><br><BR>
     <b>Category:</b><Br> 
       
     <? echo''.$cat.''; ?>
     <br><br>
     <b>Image:<br></b>
       <input name='image' size='36' maxlength='255' value="http://"><br> <br><b>Description:</b><br>
       <input name='description' size='36' maxlength='255'><br><br>
     <b>Validate:</b> 0 - Not Validated; 1 - Validated<br>
       <select size='1' name='validate' size='36' maxlength='255'>
     <option>0</option>
     <option>1</option>
     </select><br><br>
       <input type="submit" name="submit" value="Add Tutorial">  
     <input type="reset" name="Reset" value="Reset"></form>
<?php
}

?>
</div></div>
<? include('admin_footer.php'); ?>


This is what the view source is showing for the drop down:

Code:
     <select size="1" name="category" size="36" maxlength="255"><OPTION VALUE="NA" selected>Select A Category</OPTION><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option></select>     <br><br>




EDIT: i got it so the select values display the right id by adding $row = in front of the mysql_fetch_array, and also brought the number of options to the correct number, just trying to get it to display the category name now.

EDIT: lol, stuiped me, was looking for the 'name' column when i changed it to 'title' in the databse. It works now, thanks alot!
 
Last edited:
0
•••
---
 
0
•••

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back