Jump to content

"php" help with menu bar


romio

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.