Jump to content


Photo

"php" help with menu bar


  • Please log in to reply
No replies to this topic

#1 romio

romio
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 28 March 2006 - 07:08 AM

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.
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);
                    }
                }
            }
    
}

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);
            }
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users