How to make a tree view from MySQL and PHP and jquery
- by Mac Taylor
hey guys
i need to show a treeview of my categories , saved in my mysql database .
Database table :
table : cats :
columns: id,name,parent
Here is a sample of what I want the markup to be like:
<ul id="browser" class="filetree">
<li><span class="folder">Folder 1</span>
<ul>
<li><span class="file">Item 1.1</span></li>
</ul>
</li>
<li><span class="folder">Folder 2</span>
<ul>
<li><span class="folder">Subfolder 2.1</span>
<ul id="folder21">
<li><span class="file">File 2.1.1</span></li>
<li><span class="file">File 2.1.2</span></li>
</ul>
</li>
<li><span class="file">File 2.2</span></li>
</ul>
</li>
<li><span class="file">File 4</span></li>
</ul>
i used this script to show treeview :
http://www.dynamicdrive.com/dynamicindex1/treeview
now problem is in php part :
//function to build tree menu from db table test1
function tree_set($index)
{
global $menu;
$q=mysql_query("select * from cats where parent='$index'");
if(!mysql_num_rows($q))
return;
$menu .= '<ul>'."\n";
while($arr=mysql_fetch_assoc($q))
{
$menu .= '<li>';
$menu .= '<span class="file">'.$arr['name'].'</span>';//you can add another output there
$menu .=tree_set("".$arr['id']."");
$menu .= '</li>'."\n";
}
$menu.= '</ul>'."\n";
return $menu;
}
//variable $menu must be defined before the function call
$menu = '
<link rel="stylesheet" href="modules/Topics/includes/jquery.treeview.css" />
<script src="modules/Topics/includes/lib/jquery.cookie.js" type="text/javascript"></script>
<script src="modules/Topics/includes/jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript" src="modules/Topics/includes/demo/demo.js"></script>
<ul id="browser" class="filetree">'."\n";
$menu .= tree_set(0);
$menu .= '</ul>';
echo $menu;
i even asked in this forum :
http://forums.tizag.com/showthread.php?p=60649
problem is in php part of my codes that i mentioned . i cant show sub menus , i mean , really i dont know how to show sub menus
is there any chance of a pro php coder helping me here ?