[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 08-19-2006, 01:58 PM   #1 (permalink)
Senior Member
 
Join Date: Oct 2003
Location: Winnipeg, Canada
Posts: 3,472
5.31 NP$ (Donate)

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 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
-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);
     if(
$parent=='0'){
    
$cat.='<option>-'.$categ.'</option>';
}else{
    
$cat.='<option>---'.$categ.'</option>';
    }
    }
    }
    
$cat.='</select>';
killaklown is offline  
Old 08-19-2006, 02:31 PM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

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
$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
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator

Last edited by Noobie; 08-19-2006 at 02:34 PM.
Noobie is offline  
Old 08-19-2006, 02:50 PM   #3 (permalink)
Senior Member
 
Join Date: Oct 2003
Location: Winnipeg, Canada
Posts: 3,472
5.31 NP$ (Donate)

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 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>';
$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.
killaklown is offline  
Old 08-19-2006, 02:52 PM   #4 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

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, 02:58 PM   #5 (permalink)
Senior Member
 
Join Date: Oct 2003
Location: Winnipeg, Canada
Posts: 3,472
5.31 NP$ (Donate)

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 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, 03:00 PM   #6 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

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, 03:04 PM   #7 (permalink)
Senior Member
 
Join Date: Oct 2003
Location: Winnipeg, Canada
Posts: 3,472
5.31 NP$ (Donate)

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 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());
      
  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>
     </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 03:17 PM.
killaklown is offline  
Old 08-19-2006, 03:19 PM   #8 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 04:15 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85