Accordion and subcategories
I have the following php code:
- class Categories
{
var $exclude;
var $depth;
function __construct() {
$this->exclude = array();
$this->depth = 1;
}
function get_menu()
{
$nav_query = mysql_query("SELECT * FROM categories WHERE parentid='0' ORDER BY order_by ASC") or die( mysql_error() );
$tree = ""; // Clear the directory tree
$this->depth = 1; // Child level depth.
$top_level_on = 1; // What top-level category are we on?
array_push($this->exclude, 0); // Put a starting value in it
while ( $nav_row = mysql_fetch_array( $nav_query ) )
{
$goOn = 1; // Resets variable to allow us to continue building out the tree.
for( $x = 0; $x < count( $this->exclude ); $x++ )// Check to see if the new item has been used
{
if ( $this->exclude[$x] == $nav_row['categoryid'] )
{
$goOn = 0;
break; // Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
}
}
if ( $goOn == 1 )
{
#top category or in other words category who's parent_id is 0
$tree .= "<h3><a href='products.php?catid=".$nav_row['categoryid']."'><span>".$nav_row['category']."</span></a></h3>"; // Process the main tree node
array_push( $this->exclude, $nav_row['categoryid'] ); // Add to the exclusion list
if ( $nav_row['categoryid'] < 6 ){
$top_level_on = $nav_row['categoryid'];
}
$tree .= "<ul>";
$tree .= $this->build_child( $nav_row['categoryid'] ); // Start the recursive function of building the child tree
$tree .= "</ul>";
}
}
return $tree."";
// echo "</ul>";
}
function build_child($oldID){
$child_query = mysql_query( "SELECT * FROM categories WHERE parentid=" . $oldID ) or die( mysql_error() );
$querychilds = mysql_num_rows($child_query);
if ($querychilds){
$tempTree .= '<li style="list-style:none;padding:0px;">';
while ($child = mysql_fetch_array($child_query)) {
$tempTree .= '<ul>';
if ($child['categoryid'] != $child['parentid']) {
$tempTree .= "<li><a href='products.php?catid=".$child['categoryid']."'>" . $child['category'] . "</a></li>";
$this->depth++; // Incriment depth b/c we're building this child's child tree (complicated yet???)
$tempTree .= $this->build_child( $child['categoryid'] ); // Add to the temporary local tree
$this->depth--; // Decrement depth b/c we're done building the child's child tree.
array_push( $this->exclude, $child['categoryid'] ); // Add the item to the exclusion list
}
$tempTree .= '</ul>';
}
$tempTree .= "</li>";
return $tempTree; // Return the entire child tree
}
}
}
The problem that I'm having is that this creates a nice menu where the main categories are clickable and they expand and collapse, however I want the subcategories to do the same. Can anyone share some advice on this for me? I have looked everywhere...