NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Order Sub-Categories under Parent

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-19-2006, 02:58 PM THREAD STARTER               #1 (permalink)
Senior Member
 
killaklown's Avatar
Join Date: Oct 2003
Posts: 3,472
killaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to behold
 



Order Sub-Categories under Parent


How will i be able to order my sub-categories under my parent?

Right now they show up as:

-Parent 1
????: NamePros.com http://www.namepros.com/programming/229487-order-sub-categories-under-parent.html
-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 Code:
    $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);
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
     if(
$parent=='0'){
    
$cat.='<option>-'.$categ.'</option>';
 }else{
     
$cat.='<option>---'.$categ.'</option>';
    }
    }
    }
    
$cat.='</select>'
killaklown is offline  
Old 08-19-2006, 03:31 PM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



basic way:
table looks like:
category_id (int)
category_name (Varchar)
category_parent (int)

PHP Code:
$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
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
$nrs mysql_query($nsql);
while(
$childrenmysql_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
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Last edited by Noobie; 08-19-2006 at 03:34 PM.
Noobie is offline  
Old 08-19-2006, 03:50 PM THREAD STARTER               #3 (permalink)
Senior Member
 
killaklown's Avatar
Join Date: Oct 2003
Posts: 3,472
killaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to behold
 



I Edited it to work with the way my table is setup:
PHP Code:
    $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>';
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
$nsql "SELECT * FROM categories WHERE parent='".$pid."'";
$nrs mysql_query($nsql);
while(
mysql_fetch_array($nrs)){
 
$cname stripslashes($row[name]);
 
$cid stripslashes($row[id]);
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
 
$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.
killaklown is offline  
Old 08-19-2006, 03:52 PM   #4 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



PM me your site's url, i can't guess what the problem is
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-19-2006, 03:58 PM THREAD STARTER               #5 (permalink)
Senior Member
 
killaklown's Avatar
Join Date: Oct 2003
Posts: 3,472
killaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to behold
 



I just uploaded a screenshot of the part and the database:

http://img217.imageshack.us/img217/7017/catrr7.gif
killaklown is offline  
Old 08-19-2006, 04:00 PM   #6 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



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.
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-19-2006, 04:04 PM THREAD STARTER               #7 (permalink)
Senior Member
 
killaklown's Avatar
Join Date: Oct 2003
Posts: 3,472
killaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to beholdkillaklown is a splendid one to behold
 



This is the whole code for it...

PHP Code:
<? 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()); 
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
      
  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> 
     &nbsp;&nbsp;<input name='title' size='36' maxlength='255'><br><BR>
     <b>URL:<br></b>
     &nbsp;&nbsp;<input name='url' value='http://' size='36' maxlength='255'><br><BR>
     <b>Category:</b><Br> 
     &nbsp;&nbsp;
     <? echo''.$cat.''?>
     <br><br>
     <b>Image:<br></b>
     &nbsp;&nbsp;<input name='image' size='36' maxlength='255' value="http://"><br>&nbsp;<br><b>Description:</b><br>
     &nbsp;&nbsp;<input name='description' size='36' maxlength='255'><br><br>
     <b>Validate:</b> 0 - Not Validated; 1 - Validated<br>
     &nbsp;&nbsp;<select size='1' name='validate' size='36' maxlength='255'>
     <option>0</option>
     <option>1</option>
????: NamePros.com http://www.namepros.com/showthread.php?t=229487
     </select><br><br>
     &nbsp;&nbsp;<input type="submit" name="submit" value="Add Tutorial">&nbsp;&nbsp;
     <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 by killaklown; 08-19-2006 at 04:17 PM.
killaklown is offline  
Old 08-19-2006, 04:19 PM   #8 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



---
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 08:30 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger