Jump to content

Archived

This topic is now archived and is closed to further replies.

romio

"php" help with menu bar

Recommended Posts

Am currently working on php Product Catalogue system, DB is created and is running fine, small problem with the menu though, each category might have many sub_categories, and here were the problem starts, if i have cat_1 with sub cat_1_1 and cat_1_2 and cat_2 with sub cat_2_1 then codes works fine, i need to have a general function that would work on all depth, i cant just keep creating a new sql or a function code for every sub_categories! so i though of a recursive function or an array.. Any help would be appreciated.
[code]
function find_root_categories()
{
    $catID = $_GET['cat'];
        
        $find_parent_categories = "SELECT node.category_id ,node.name, (COUNT(parent.name) - 1) AS depth
        FROM categories AS node, categories AS parent
        WHERE node.lft
            BETWEEN parent.lft AND parent.rgt
        GROUP BY node.name
        HAVING depth=1 ORDER BY node.lft";
        
    $result_parent_categories = mysql_query($find_parent_categories);
    $num_rows_parent_categories = mysql_num_rows($result_parent_categories);

    if(!($num_rows_parent_categories))
        {
            echo"<tr>
                    <td valign=top align=center>No Records To Display</td>
                </tr>";
        }
            else
            {
                while($row = mysql_fetch_array($result_parent_categories))
                {
                      $name= $row['name'];
                      $catid = $row['category_id'];
                    echo"
                            <a href='?cat=$catid'>$name</a><br />
                        ";
                    if($catID == $catid){
                        
                        get_sub_cat($catID);
                    }
                }
            }
    
}
[/code]
[code]

function get_sub_cat($catID){
    
  
   $sql = "select * from categories where parent = $catID";
   $result = mysql_query($sql);
   $row = mysql_fetch_array($result);
      
   // now, retrieve all descendants of the $root node
   $result = mysql_query($sql);

    while($row = mysql_fetch_array($result))
    {
          $name= $row['name'];
          $catid = $row['category_id'];
        echo"
                &nbsp;&nbsp;&nbsp;<a href='?cat=$catid'>$name</a><br />
            ";
            if($catID == $catid){
                        get_sub_cat($catid);
            }
    }
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.