1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Colombo, Western, Sri Lanka
    Rep Power
    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 :

    $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>";


    $subcategory=mysql_query("select name from categories where parent_id=$pid");



    while ($j<$num2){

    echo "<p>";

    print (" ");

    echo mysql_result($subcategory,$j,"name");





    This is what I have in my mind.

    Immediately after the first 'while' there should be something like this :


    if ($parent=0) :

    and just before $i++:}


    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. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Niagara Falls (On the wrong side of the gorge)
    Rep Power
    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.

Similar Threads

  1. A mixed foreach loop
    By Datamike in forum Dev Shed Lounge
    Replies: 11
    Last Post: December 17th, 2003, 08:40 PM
  2. Alternate color - loop inside a loop?
    By hauge in forum PHP Development
    Replies: 6
    Last Post: December 2nd, 2003, 12:08 PM
  3. Getting results from one loop into another loop
    By camcim in forum PHP Development
    Replies: 1
    Last Post: September 26th, 2003, 08:26 AM
  4. pointer array in while loop
    By ppp0 in forum C Programming
    Replies: 4
    Last Post: September 1st, 2003, 09:17 AM
  5. PHP: create tables w/ loop & iterate functions ?
    By dmonk in forum Beginner Programming
    Replies: 1
    Last Post: July 6th, 2003, 10:41 AM

IMN logo majestic logo threadwatch logo seochat tools logo