Jump to content


Photo

Multilevel Menu in PHP/Mysql


  • Please log in to reply
6 replies to this topic

#1 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 24 August 2008 - 02:53 PM

Hy,

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.


#2 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 30 August 2008 - 05:01 PM

I've found an excellent example explaining this issue:

http://crisp.tweakbl...-one-query.html

#3 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 03 September 2008 - 05:47 AM

The problem is that i can't manage to make it expandable i don't want to use javascript i want to make it with conditions how oscommerce has.

<?php
if ( $_GET['id_cat'] ) {
 // display subcat
 if ( $_GET['id_subcat'] ) {
  // display subsubcat
 }
}
?>


Thanks

#4 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 03 September 2008 - 02:14 PM

I don't really know what oscommerce is. Or what menu you mean.

But you can use the $_GET options to fetch an id. And build the menu from the id, and the options/maps underneath that.

<?php
echo buildMenu(0, $menuData);
?>

Look at that code. The 0 (zero) in the function call represents the parent id. If you set the 0 to $_GET['parent_id']. All the options that are underneath that parent id will be displayed.

<?php
echo buildMenu($_GET['parent_id'], $menuData);
?>

I'm sorry if I didn't totaly understand what you were trying to ask, but I'm trying my best over here :P.

#5 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 03 September 2008 - 05:33 PM

Here is a link to oscommerce download page: http://www.oscommerc...tions/downloads

#6 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 September 2008 - 11:10 PM

My understanding of the way this should work:

Initial page loads, the top-level menu is displayed.
One of the links in thte TLM is clicked, the page reloads, and a sub-menu for the associated link is displayed.

Correct?

If that's the case, and you don't want to use JS, then you'll need a nested loop. Since i have no idea what is going on in your code, try this:
function menu($parent, $level){
	$sql = mysql_query("SELECT * FROM `categories` WHERE `parent_id` = ".(int)$parent);

	echo '<ul>';
	while($r = mysql_fetch_assoc($sql)){
		echo '<li class="menu"><a href="?'.(($level > 0) $_GET['query_string'].'&amp;c'.$level.'=' : 'c0=').$r['categories_id'].'">'.$r['categories_name'].'</a>';
		if($_GET['c'.$level] == $r['categories_id']){
			menu($r['categories_id'], $level+1);
		}
	}
}

menu(0, 0);
If you don't understand what is going on, feel free to ask for an explaination.
If it doesn't work, please provide a link to an example page or the resulting source code (link being preferable).

Warning: I make typos :P

#7 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 12 September 2008 - 10:17 AM

Thanks for your help i'll try it later because i have some problems with my pc :(. I'll modify this post to tell you the result.


Thanks :lol:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users