473,216 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

Mysql Php dynamic menu

Hello there,
I am trying to write a script that will create dynamically a menu from MySQL
database.
The database table is a product categories table and it has the folowing
filed:
category_id, parent_category_id, category_name (some other fileds are there
as well but these are enough to figure out the logic)

I know that I have to store all the product_categories records in a temp
array and then some how go through them and store the level of each record
in the menu....

If you have done something like that I suppose you know what I mean and I
would much appreciate to give me a hint here ...
I had a look in the phpLayersMenu from sourceforge but it is really advanced
OO code and I cannot understand the logic....

My target is to create a Tree menu with no pre - defined number of
records...

Thanks in advance.
Jul 17 '05 #1
3 12575
I don't know how far back to start an answer. Do you know how to create
a regular, non-tree, dynamic menu from the MySQL DB for only the top
level categories (i.e. the categories with parent_category_id=0)?

Jul 17 '05 #2
I have writen a class that does what I wanted except the UL LI part...
anyway .I am going to paste you the code here.
The logic of this script is part of phpLayersMenu in sourceforge but much
simpler.
If at anytime you redevelop that code, I would like to send me the changes
....

As far as I know it can display unlimited levels of menus.

the only problem with the ULs and LIs is that in order to make a DHTML or
plain CSS menu
You need the following structure:
<ul>
<li>Category
<ul>
<li>SubCategory</li>
<li>SubCategory
<ul>
<li>SubSubCategory</li>
<li>SubSubCategory</li>
</ul>
</li>
<li>SubCategory</li>
</ul>
</li>
<li>Category
<ul>
<li>SubCategory</li>
<li>SubCategory</li>
</ul>
</li>
</ul>
*****************************************
My script,
Closes every LI
so it creates the folowing:
(THAT NEEDS fixing to create UL LIs as above)
<ul>
<li></li>
<ul>
<li></li>
<li></li>
<ul>
<li></li>
</ul>
<li></li>
</ul>
</ul>

**************************
class menu
{
var $parent_id=0;
var $level=0;
var $tree = array();
var $nodesCount=0;
var $firstItem=1;
var $lastItem=0;
var $maxLevel=0;
var $firstLevelCnt=0;
var $i=0;
var $x = array();

function menu()
{
$this->nodesCount = 0;
$this->tree = array();
$this->treecnt = array();
$this->maxLevel = 0;
$this->firstLevelCnt = array();
$this->firstItem = array();
$this->lastItem = array();
}

function scanTableForMenu()
{
$dbresult = mysql_query('SELECT * FROM product_categories
WHERE product_categories_id != 0
');
$this->tmpArray = array();
while ($row = mysql_fetch_array($dbresult))
{
$this->tmpArray[$row['product_categories_id']]['parent_id'] =
$row['product_categories_parent_id'];
$this->tmpArray[$row['product_categories_id']]['name'] =
$row['product_categories_name'];
$this->tmpArray[$row['product_categories_id']]['body'] =
$row['product_categories_body'];
$this->tmpArray[$row['product_categories_id']]['img'] =
$row['product_categories_img'];
}
$this->depth($this->tmpArray,0,0);
echo "<ul>";
$this->displayMenu($this->tree);
echo "</ul>";
}
function depth($tmpArray,$parent_id,$level)
{
reset ($tmpArray);
foreach($tmpArray as $key => $value)
{
if ($value['parent_id'] == $parent_id)
{
unset($tmpArray[$key]);
unset($this->tmpArray[$key]);
$cnt = count($this->tree) + 1;
$this->tree[$key]['level'] = $level;
$this->tree[$key]['parent'] = $value['parent_id'];
$this->tree[$key]['name'] = $value['name'];
$this->tree[$key]['body'] = $value['body'];
$this->tree[$key]['img'] = $value['img'];
unset($value);
if($key !=$parent_id)
$this->depth($this->tmpArray,$key,$level+1);
}
}
}
function displayMenu($tree,$currentLevel = 0)
{
//echo "<pre>";
//print_r($this->tree);
//echo "</pre>";
while (list($key, $value) = each($this->tree))// for($ii = 1;$ii
<=$this->cntFirstlevel;$ii++)
{
if($this->tree[$key]['level']>$currentLevel)
echo "<ul>";
if($this->tree[$key]['level'] == $currentLevel)
{
echo "<li>";
echo "<a href=\"?cat_id=$key\">".$this->tree[$key]['name']."</a>";

}
elseif($this->tree[$key]['level'] == $currentLevel+1)
{
$this->displayMenu($this->tree,$currentLevel+1);
}
else
{
for($i=0;$i<$currentLevel;$i++)
{
echo "</li>";
echo "</ul>";
}
$this->displayMenu($this->tree,$currentLevel=0);
}
unset($this->tree[$key]);
}
}
}
Jul 17 '05 #3
Following on from Angelos's message. . .
I have writen a class that does what I wanted except the UL LI part...
anyway .I am going to paste you the code here.
The logic of this script is part of phpLayersMenu in sourceforge but much
simpler.
If at anytime you redevelop that code, I would like to send me the changes
...

As far as I know it can display unlimited levels of menus.


It is one thing to be able to generate a 'tree' menu with nodes, sub
nodes etc but another to be able to open and shut dynamically using
javascript. What I have done is to write a class (actually 2 classes )
that interfaces with Tigra Tree ['free']
(www.softcomplex.com/products/tigra_menu_tree/). I've added the ability
to use templates at different levels. The test program below should
give you a flavour. Ask if you want the code (11k too big for news)
<?php
/*
menu tree
---------

This example populates a 2-level tree showing employees
by department. It illustrates templates for links.

Note the order in which items are added to the tree.
Always add item to tree/parent node BEFORE adding children to it
Nodes are added as references so unset() before re-using a node

Files required
../cl/menutree.phpc
../tools/mnu/tree.js
../tools/mnu/tree_tpl.js
graphic files as specified in tree_tpl.js

*/
require_once('../cl/menutree.phpc');

// dummy data
$departments=array();
$e1=array(1=>'Peter fox',2=>'Sally Fox',3=>'Geoffrey Fox');
$e2=array(11=>'Teddy fox',12=>'Jean Fox');
$departments[1]=array('Sales',$e1);
$departments[10]=array('Production',$e2);
// set up tree and organise templates
$t = new menuTree();
$t->AddLinkTemplate(1,'');
$t->AddLinkTemplate(2,'department.php?DEPT=[DID]');
$t->AddLinkTemplate(3,'employee.php?EMPNO=[EID]');

// highest level is always shown and doesn't collapse
$tnode = new treeNode('Employees by department');
$t->AddNode($tnode);

// populate tree
foreach($departments as $did=>$dept){
$departmentName=$dept[0];
$dnode = new treeNode($departmentName,array('DID'=>$did));
$tnode->AddChild($dnode);
$employeesOfDept=$dept[1];
foreach($employeesOfDept as $eid=>$employeeName){
$dnode->AddChild(new treeNode($employeeName,array('EID'=>$eid)));
}
unset($dnode);
}

// illustrate bypassing template using blank for data key
$snode = new treeNode('something special',array(''=>'myhomepage.php'));
$tnode->AddChild($snode);

// show it
print($t->CompleteMenu());

?>
--
PETER FOX Not the same since the poster business went to the wall
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dr. zoidberg | last post by:
Hello, I'm creating simple menu. MySQL: +----+-----+----------------------+ | id | sid | Title | +----+-----+----------------------+ | 1 | 0 | Main Menu 1 | 2 | 0 | Main Menu 2 | ...
4
by: Stromboli | last post by:
hi people, My problem is that I need to build a dynamic menu (preferably that works in all the browsers) that appears when I mouseover a certain link. The problem is that I have to declare...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: vikram | last post by:
I have to design a page which contains a dynamic generated menu at left side.Menu will be generated once a user log in and will remain as it is for the rest of the user session. Problem is that...
3
by: Angelos | last post by:
Hello again, I have this dynamic menu and I want to change the order of the menu items... I added a column in the database wich has an integer value for ordering the menuitems. But the only way...
4
by: snowweb | last post by:
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...
3
by: RahimAsif | last post by:
I am writing an application that requires the a portion of the main menu to be dynamic. The menu has file, panels, view files and help across the top. The view files sub menu needs to be...
1
by: edfialk | last post by:
Hi all, I'm desperately trying to get a simple mysql connection working in php 4.3.9 and I think I have a doozy for you guys. First of all, I didn't set up ANY of this system, I'm just working...
3
by: jaddi1 | last post by:
Hi, I am trying to make a multi-level drop-down menu similar to what is seen here: http://www.cssplay.co.uk/menus/simple_vertical.html. My problem is that some of the menu will be populated from...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.