Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Category & Sub Categories

    PHP Code:
            $result mysql_query("SELECT * FROM sc_phpscart_category WHERE parent='0' ORDER BY category ASC");
            
    $count=0;
            if(
    mysql_num_rows($result))
            {
                echo 
    "<strong>Sub-Category of: <select name='parent' style='background-color: transparent'>\n";
                echo 
    "<option value='".$returnID."' selected>".$info->getCatName($returnID)."</option>\n";
                echo 
    "<option value='0'>-- Top Level --</option>\n";
                while(
    $row=mysql_fetch_array($result))
                {
                    
    $count=$count+1;
                    echo 
    "<option value='".$row["id"]."'>".$row["category"]."</option>\n";
                    
                    
    $result2 mysql_query("SELECT * FROM sc_phpscart_category WHERE parent='".$row["id"]."' ORDER BY category ASC");
                    while(
    $row2=mysql_fetch_array($result2))
                    {
                        
    //$count=$count+1;
                        
    echo "<option value='".$row2["id"]."'>|&nbsp;&nbsp;&nbsp;- ".$row2["category"]."</option>\n";
                        
    $result3 mysql_query("SELECT * FROM sc_phpscart_category WHERE parent='".$row2["id"]."' ORDER BY category ASC");
                        while(
    $row3=mysql_fetch_array($result3))
                        {
                            
    //$count=$count+1;
                            
    echo "<option value='".$row3["id"]."'>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;- ".$row3["category"]."</option>\n";
                        }
                    }

                }
                echo 
    '</select>';
            }
            else
            {
                echo 
    '<div align="center"><font size="'.$font_size.'" face="'.$font_face.'">'.$error["nogigs"].'</font></div>';
            } 
    Alright... to help with the last code I was doing I have this code which takes makes a drop list of categories and sub categories. However it is only limited to 3 sub categories and uses many mysql_queries. For example:
    Code:
    Motorcycles
    | Harley-Davidson
         | Parts
             | Leathers
             | T-Shirts
         | Clothes
    | Honda
         | Parts
         | Lights
    | Yamaha
    I would like to be able to make a list like that (dropdown) without using too many mysql_queries and that isn't limited to only 3 sub categories so I may have many sub-categories. How would I go about doing this more efficently than the way I am doing.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i recon you would need a recursive function something like this,.
    PHP Code:
    <?php
    function display_children($parent$level) {
          
    $result mysql_query("SELECT * FROM sc_phpscart_category WHERE parent='".$parent."' ORDER BY category ASC");
       while (
    $row mysql_fetch_array($result)) {     
           echo 
    "<option value='".$row["id"]."'>";
           
           for(
    $i $i $level$i++)
           {
           echo 
    "|&nbsp;&nbsp;&nbsp;";
           }
           
           echo 
    $row["category"]."</option>\n";
           
    display_children($row['title'], $level+1);
       }
    }

    $result mysql_query("SELECT * FROM sc_phpscart_category WHERE parent='0' ORDER BY category ASC");
    if(
    mysql_num_rows($result))
    {
        echo 
    "<strong>Sub-Category of: <select name='parent' style='background-color: transparent'>\n";
        echo 
    "<option value='".$returnID."' selected>".$info->getCatName($returnID)."</option>\n";
        echo 
    "<option value='0'>-- Top Level --</option>\n";
                
        while(
    $row=mysql_fetch_array($result))
        {   
        
    display_children($row["id"],0);
        }
         echo 
    '</select>';
    }
    ?>

    please note i can't say for sure that code works. but thats the basic idea
    Last edited by MRMAN; 05-08-2006 at 11:57 AM.

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Well, the code doesn't work... seems to have an endless loop as it doesn't stop running, then finally times out. It gives me a long list like this:

    Code:
    -- Top List --
    Bombardier
    | ATV's
    | | ATV's
    | | | ATV's
    | | | | ATV's
    | | | | | ATV's
    | | | | | | ATV's
    | | | | | | | ATV's
    | | | | | | | | ATV's
    | | | | | | | | | ATV's
    Mind ya, there are alot more rows of ATV's than that....
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Yea, nevermind... I got it working.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Looping a query to your database inside another database query is very intensive. Best to query your database in a single shot and then use php to determine your layout. check the link in my signature if you want to see an example. it is easily expandable by changing the database query and adding another check on the subcategories.

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Well,
    this is actually working rather well and doesn't take too long to load even with alot of categories. However, I am using it to allow users to edit a category.

    For example: A users goes to a certain category or sub category then can modify the title of the category and which category it is located under. However, the current set up allows you to make the category a sub category of one of it's sub categories. Which would then provide an endless loops of category selection when visiting the website.

    I hope that made sense...
    In a more visual approach:
    Code:
    Before
    ------
    Cars
      Chevy
      Ford
    Motorcycles
      Harley-Davidson
         Clothes
            Leathers
         Parts
      Honda
         Clothes
    
    After
    -----
    Cars
      Chevy
      Ford
    Motorcycles
      ----------
         Clothes
            Harley-Davidson
            Leathers
         Parts
      Honda
         Clothes
    And since Clothes originally points to "Harley-Davidson" and Harley-Davidson isn't a parent of the root (nothing) then you can't change it. How can I get the list to not produce the children of the category you are currently in/editing?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    *Da Bump*
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Use MPTT or some other method of storing hierarchical data.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Regular Coder
    Join Date
    Jul 2006
    Posts
    174
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by guelphdad View Post
    check the link in my signature if you want to see an example.
    What happened to those links you had listed in the past?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •