I have a problem i can't get to the head of it. I want to make a multilevel menu in php/mysql
Table: categories
Columns: categories_id, parent_id, sort_order, categories_name
The master parent_id is 0
Values:
- 1, 0, 1, MainCat1
- 2, 0, 2, MainCat2
- 3, 0, 3, MainCat3
- 4, 1, 0, SubCat1
- 5, 2, 0, SubCat2
- 6, 3, 0, SubCat3
- 7, 4, 0, SubSubCat1
- 8, 5, 0, SubSubCat2
- 9, 6, 0, SubSubCat3
............................
The output should be:
MainCat1
- SubCat1
-- SubSubCat1
MainCat2
- SubCat2
-- SubSubCat2
MainCat3
- SubCat3
-- SubSubCat3
......................
I did something but i can't figure it out how to make it to go to the next levels i managed to get it just to the SubCat1 level and there it displays all the levels on level 1
Thanks.
I wrote this code i've found it on another website and now i want to make it expandable but not in javascript but by parent_id or other key
[codebox] $q = mysql_query("SELECT * FROM categories");
while ( $r = mysql_fetch_assoc($q) ) {
$m_a[$r['categories_id']] = array('categories_name' => $r['categories_name'], 'parent_id' => $r['parent_id']);
}
function menu($parent) {
global $m_a;
$h_c = false;
foreach ($m_a as $k => $v ) {
if ( $v['parent_id'] == $parent ) {
if ( $h_c === false ) {
$h_c = true;
echo "<ul>\n";
}
echo '<li class="menu"><a href="?c='.$k.'">'.$v['categories_name'].'</a>';
if ( isset($_GET['c']) ) {
menu($k);
}
echo "</li>\n";
}
}
if ( $h_c === true ) {
echo "</ul>\n";
}
}
menu(0);
[/codebox]
I want to make a multilevel menu like OsCommerce has.
Thanks
Edited by rc69, 30 August 2008 - 04:29 PM.