954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php/mysql dynamic multi-level menu problem

I am trying to implement a CSS hierarchical unfolding menu on a site. The thing is, it needs to be dynamically populated from the results of a database query. I previously had the menu working but then it was ‘hard coded’ and not built on the fly.

Menu description:

2 top level items “Company and “Products (we will ignore “Company since it is still hard coded and not causing a problem.

Below “Products we have hard coded “By Manufacturer. So you hover over “Products and it unfolds and “By Manufacturer is visible. If you hover over that, a list of manufacturers should open to the right. This list is extracted from the data base using SELECT DISTINCT.

Then if you hover over a manufacturer, another level unfolds which should contain the products of that manufacturer.

My problem is that the list of products includes all products in the database, not just those from the relevant manufacturer. The menus of the other manufacturers are empty.

The manufacturer under which all the products appear, is the manufacturer of the first product in the database.

[PHP]
<? require("inc/connect.txt");
/* Connecting to a database and retrieve data */
$mysql_access = mysql_connect("localhost", "$un", "$pw") or die("Error connecting to database server: ".mysql_error());
mysql_select_db($db, $mysql_access) or die("Error connecting to database: ".mysql_error());//always gotta do some error checking...

/* Get list of unique manufacturers */
$result_manu = mysql_query("SELECT DISTINCT `Manufacturer` FROM `products`");

/* Get list of products and details */
$result = mysql_query("SELECT `ProdID`,`Manufacturer`, `NameModel` FROM `products`");

//always gotta do some error checking...
if (!$result)
{exit("Error in SQL");} ?>


[/PHP]

Below is the code which is rendered by the browser when displayed without the appropriate CSS stylesheet and JavaScript that makes it work.
[HTML]


[/HTML]

...and this is the code that I am aiming to have rendered..

[HTML]


[/HTML]

I've a feeling that I'm close. Can anyone see my mistake please?

kind regards

pete

snowweb
Newbie Poster
8 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks, this is now resolved.

snowweb
Newbie Poster
8 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Just for anyone else who happens upon this thread and are as irritated as I am with folks who post "It's Fixed", but don't have the courtesy to say how they fixed it, the original poster appears to have gotten "it fixed" over here:

http://www.thescripts.com/forum/thread449155.html

SoccerDad
Newbie Poster
1 post since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

and why should I waste my time explaining how I fixed it when NO-ONE on this forum gave me even the slightest help?

- Courtesy works both ways... not just one!

snowweb
Newbie Poster
8 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

and why should I waste my time explaining how I fixed it when NO-ONE on this forum gave me even the slightest help?

- Courtesy works both ways... not just one!

Well, people might not be knowing the answer for your question. You can't expect them to know the answer for everything. Can you ? And If you had posted your 'fix', it would have helped others who might have encountered the same problem. If your attitude is, 'why should I waste my time explaining blah blah blah', why do you expect help from others ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

LOL, I'm actually supriced he replayed as the original post is 2 years old now

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
LOL, I'm actually supriced he replayed as the original post is 2 years old now

:P Prolly he came back today after a long break !

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

We can create multi level drop down using php/mysql .
we can also create up to unlimited level of drop down using this code.

first i am creating a mysql table

CREATE TABLE IF NOT EXISTS `navi_links` (
  `link_id` smallint(6) NOT NULL auto_increment,
  `link_parent` smallint(6) NOT NULL,
  `link_url` varchar(255) collate latin1_general_ci NOT NULL,
  `link_name` varchar(255) collate latin1_general_ci NOT NULL,
  `display_order` smallint(6) NOT NULL,
  PRIMARY KEY  (`link_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;

now i am creating one recursive function which i will call number of times till the parent exist. // prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
$result = mysql_query("SELECT link_id id, link_parent parentId, link_name name FROM navi_links ORDER BY link_parent");

while ($menuItem = mysql_fetch_assoc($result))
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
$menuClass= ($parentId==0) ? ' class="menu" id="menu"' : '';
$parent= ($parentId==0) ? 0 : 1;
$html = "<ul{$menuClass}>\n";

foreach ($menuData['parents'][$parentId] as $itemId)
{
//subment
$result=mysql_query("select * from navi_links where link_parent='$itemId'");
if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
$subm =' class="sub"';
}else{
$subm ='';
}
//end

$menu = $parentId == 0 ? ' class="menulink"' : ''; //class of main menu
$html .= '<li>' . "<a{$subm}{$menu} href=\"#\" title=\"{$row['title']}\">{$menuData['items'][$itemId]['name']}</a>";

// find childitems recursively
$html .= buildMenu($itemId, $menuData);

$html .= '</li>';
}
$html .= '</ul>';
}

return $html;
}

// output the menu
echo buildMenu(0, $menuData);

narasingh
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 1
 

Perfect!
We can create multi level drop down using php/mysql .
we can also create up to unlimited level of drop down using this code.

first i am creating a mysql table

CREATE TABLE IF NOT EXISTS `navi_links` (
  `link_id` smallint(6) NOT NULL auto_increment,
  `link_parent` smallint(6) NOT NULL,
  `link_url` varchar(255) collate latin1_general_ci NOT NULL,
  `link_name` varchar(255) collate latin1_general_ci NOT NULL,
  `display_order` smallint(6) NOT NULL,
  PRIMARY KEY  (`link_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;

now i am creating one recursive function which i will call number of times till the parent exist. // prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
$result = mysql_query("SELECT link_id id, link_parent parentId, link_name name FROM navi_links ORDER BY link_parent");

while ($menuItem = mysql_fetch_assoc($result))
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
$menuClass= ($parentId==0) ? ' class="menu" id="menu"' : '';
$parent= ($parentId==0) ? 0 : 1;
$html = "<ul{$menuClass}>\n";

foreach ($menuData['parents'][$parentId] as $itemId)
{
//subment
$result=mysql_query("select * from navi_links where link_parent='$itemId'");
if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
$subm =' class="sub"';
}else{
$subm ='';
}
//end

$menu = $parentId == 0 ? ' class="menulink"' : ''; //class of main menu
$html .= '<li>' . "<a{$subm}{$menu} href=\"#\" title=\"{$row['title']}\">{$menuData['items'][$itemId]['name']}</a>";

// find childitems recursively
$html .= buildMenu($itemId, $menuData);

$html .= '</li>';
}
$html .= '</ul>';
}

return $html;
}

// output the menu
echo buildMenu(0, $menuData);

kodegeek
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

If you want to see a tutorial on how to build a dynamic php menu in Dreamweaver without coding you can see it here

extendstudio
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

looks quite great! Thankx : )

alex71
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

its very super but dont no how to set the links for menus and sub menus from database

janajay
Newbie Poster
2 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

its very super but dont no how to set the links for menus and sub menus from database

please help me in this

janajay
Newbie Poster
2 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You