473,484 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12634
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
8459
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
5172
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
17624
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
1744
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
4931
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
19077
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
4759
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
2833
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
4595
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
7082
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6813
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4845
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1359
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
235
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.