How to display PHP tree?

  • Thread starter Thread starter shiva0101 m
  • Start date Start date
S

shiva0101 m

Guest
Hi,

I have to display a tree from database. I searched and found the below code. It is working very well. I am displaying the contents by using templates. But, the below code echo's the contents. Can someone tell me how to store the contents in a variable and display the contents.

I tried by i could not display all the contents.



function tree_set($index)
{
$q=mysql_query("select * from categories where parent_id=$index");
if(!mysql_num_rows($q))
return;
echo '<ul>';
while($arr=mysql_fetch_assoc($q))
{
echo '<li>';
echo $arr['name'];//you can add another output there
tree_set($arr['id']);
echo '</li>';
}
echo '</ul>';
}

tree_set(0);



Thanks
 
function tree_set($index)
{
$query="select * from categories where ParentId=$index";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
$output = '';
if(!$num_rows)
return;
else{
$output = '<ul>';
while($arr=mysql_fetch_assoc($result))
{
$output .= '<li>';
$output .= $arr['CategoryName'];//you can add another output there
$output .= tree_set($arr['Id']);
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
}

$output = tree_set(0);
echo $output;

I believe you are using smarty.

In that case, assign the variable $output to a smarty variable.

eg:
$smaryObj->assign('output',$output);

Hope this helps.
 
Back
Top