473,387 Members | 1,892 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,387 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 12623
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.