![]() |
Categories and sub categories - recursion
Hi.
Im new here, so hello to eveybody :) Im at my wits end. I can make a perfectly working recursion script that output my categories and sub categories with no problem. But making the categories be outputted in an un-ordered list (CSS) doesn't work. DATABASE Code:
CREATE TABLE `categories` (The structure basically uses: ID : Unique identification PARENT : Referer to the parent category The rest is just various data that I use for other things. Recursion function PHP Code:
Category 1 - Sub Category 2 - Sub Category 3 - - Sub Category 4 Category 5 - Sub Category 6 What I need is: HTML Code:
<ul id="categories" class="treeview">Because the ending </li> and </ul> will be missing the right spots. PHP Code:
|
Try moving the ul tag outside the loop. That should correctly render the list.
You are looping thru the items that belong in the list, the recursive call will take care of wrapping the items in the ul. i didn't check the logic just moved the ul tag. <code> class Categories { public function categories_tree($country_id='',$parent='0',$spacer ='') { $db = new Db(); $sql = $db->query("SELECT c.*,cd.name ". "FROM categories AS c,categories_description AS cd ". "WHERE c.id=cd.category_id ". "AND c.parent='$parent' ". "AND cd.country_id='$country_id' ". "ORDER BY c.parent,c.type,c.sortorder"); $num = $db->count_rows($sql); $spacer .= '- '; echo '<ul>'; while(FALSE !== ($row = $db->fetchArray($sql))){ if($num==0 || $row['parent']=='0') { $spacer = ''; } echo '<li><span>', $spacer, '<a href="productlisting.php?cid=', $row['id'], '">', $row['name'], '</a></span>'; $this->categories_tree($country_id,$row['id'],$spacer); echo '</li>'; } echo '</ul>'; } } </code> |
it what I currently looking for as well, can any one help. I am currently use on my project http://www.wcscambodia.org, but wrote in long php code.
|
| All times are GMT. The time now is 03:08 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0