Jump to content

About tree menu. Recursive function, [ To justsomeguy ]


dhimo

Recommended Posts

Hi justsomeguy. First of all thanks for your help. I see that you give a lot of help in this forum. If u remember u helped with the code below. You think you can give me some more tips how to use html table, divs or <li> to make the menu look nicer and to show the corresponding child menu when i click on the parent. Newbie u know. Thanks in advance

function show_categories($parent = 0, $depth = 0){  $retval = "<br />";  $indentval = "  ";  $result = mysql_query("SELECT * FROM tbl_category WHERE cat_parent_id='$parent'");  while ($row = mysql_fetch_assoc($result))  {  extract($row);  	for ($i = 0; $i < $depth; $i++)	  $retval .= $indentval;	  $retval .= "<a href=\"?$cat_id\">".$cat_name."</a>\n";	  $retval .= show_categories($cat_id, $depth + 1);	  	  }    return $retval;}

Link to comment
Share on other sites

The way I normally do it is to use divs with increasing padding on the left for the indent. So this would do that:

function show_categories($parent = 0, $depth = 0){  $retval = "<br />";    $padding = $depth * 10;  $result = mysql_query("SELECT * FROM tbl_category WHERE cat_parent_id='$parent'");  while ($row = mysql_fetch_assoc($result))  {	extract($row);	$retval .= "<div style=\"padding-left: {$padding}px;\"><a href=\"?$cat_id\">".$cat_name."</a></div>";	$retval .= show_categories($cat_id, $depth + 1);  }  return $retval;}

About clicking on a parent to see the children, are you saying you want to have an expandable menu where it only shows the root categories first, and you click one and the stuff under it pops up? The best way to do that would be some javascript that will show and hide things. In that case, you would need to put all of the children for a category in a div as well.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...