|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
categories table info :
id: unique auto increment parent_id: all master categories =0 sub categories = id of its master category name : name of category The script I've written is supposed to each master category in the table followed immediately by any sub-categories it may have. If a particular master category does not have any subcategories it should fetch the next master category. My script works fine up to the point where the table contains a few subcategories before the next master category. At this point it lists out the subcategory (which has already been listed earlier within a master category) again assuming that it is a master category. All I need if an if or while statement in my script. I've tried different variations, but nothing worked. Here's the script : <html> <body> <?php mysql_connect(.....); mysql_select_db(...); $result = mysql("select * from categories where id!=0"); $num = mysql_numrows($result); $i = 0; while($i < $num) { echo "<p>"; echo mysql_result($result,$i,"name"); echo "<p>"; $pid=mysql_result($result,$i,"id"); $subcategory=mysql_query("select name from categories where parent_id=$pid"); $num2=mysql_numrows($subcategory); $j=0; while ($j<$num2){ echo "<p>"; print (" "); echo mysql_result($subcategory,$j,"name"); $j++;} $i++;} ?> </body> </html> This is what I have in my mind. Immediately after the first 'while' there should be something like this : $parent=mysql_result($result,$i,"parent_id"); if ($parent=0) : and just before $i++:} endif; That's the idea. But it doesn't work. I get a blank screen. Somebody please help me Thanks a lot to anyone who will be able to help me out. |
|
#2
|
|||
|
|||
|
You are selecting all categories in your first query, so sub-categories will be included. Then you select sub-categories again by the parent id. Naturally, they'll show up twice.
Modify your first query to exclude non-toplevel categories. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > condition within a loop?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|