Hi guys,
Having a bit of difficulty with the tree menu i have so far.
Everything displays correctly at the moment except i want to add a feature that will collapse/uncollapse the menus. ie. the cateogories with the
<img border="0" src="minus.gif" style="cursor:hand">
next to it
Here is the code so far:
<table width="153" border="0" cellpadding="0" cellspacing="0" bgcolor="#4E4E4E">
<tr>
<td><img src="images/what_we_got_btn.jpg" width="153" height="24"></td>
</tr><td>
<?php
function menu_level($parent, $indent, $connection)
{
if (isset($parent))
{
$query = "SELECT * FROM categories WHERE catParent = $parent";
}
if (!($result = @ mysql_query ($query, $connection)))
showerror();
else
{
$counter = 0;
while ($row = mysql_fetch_array($result))
{
$query_2 = mysql_query ("SELECT * FROM categories WHERE catParent = '" . $row['catID'] . "'");
$query_4 = mysql_num_rows($query_2);
if ($query_4 == 0){
echo "<TR>";
echo " <TD STYLE=text-align:left;'>";
echo " <a class='rightNav' href='product_list.php?catID=" . $row['catID'] . "'>";
echo " <p align='left' STYLE='text-indent: $indent ; font-weight: bold'>" . $row['catName'];
echo " </a>";
echo " </TD>";
echo "</TR>";
} else {
echo "<TR>";
?>
<td>
<img border="0" src="minus.gif" style="cursor:hand">
<span class='rightNav' STYLE='text-indent: <? echo $indent ?>'><? echo $row['catName'] ?></span>
<?
echo " </TD>";
echo "</TR>";
?>
<tr>
<td height="5" valign="top" background="images/right_nav_dash.gif"><img src="images/right_nav_dash.gif" width="3" height="5"></td>
</tr>
<?
}
$sub_query = "SELECT * FROM categories WHERE catParent = " . $row['catID'] . " LIMIT 0, 1";
if ($sub_result = @ mysql_query ($sub_query, $connection))
{
$sub_indent = $indent + 10;
menu_level($row['catID'], $sub_indent, $connection);
}
}
}
}
?>
<TABLE CLASS='block' STYLE="vertical-align:top">
<?php
include ('inc/db_conn_open.php');
if (!($connection = @ mysql_pconnect ($db_host, $db_username, $db_password)))
showerror();
if (!(mysql_select_db ($db_name, $connection)))
showerror();
menu_level(0,0,$connection);
?>
</TABLE>
</td>
</tr>
</TABLE>
And this is my database table structure:
catID catParent catName
1 0 Guys
2 0 Girls
3 0 Kids
4 0 Accessories
5 0 Limited Ed.
6 0 Specials
7 1 T-Shirts/Singlets
8 1 Jackets/Jumpers
9 1 Pants/Shorts
10 2 Jackets/Jumpers
11 2 T-Shirts/Singlets
12 2 Skirts/Pants
13 3 Boys
14 3 Girls
15 13 Jackets/Jumpers
16 13 T-Shirts/Singlets
17 13 Pants/Shorts
18 14 Jackets/Jumpers
19 14 T-Shirts/Singlets
20 14 Skirts/Pants
Any help greatly appreciated.
macca