473,785 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ 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_categor ies 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 12671
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>SubCatego ry
<ul>
<li>SubSubCateg ory</li>
<li>SubSubCateg ory</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->firstLevelCn t = array();
$this->firstItem = array();
$this->lastItem = array();
}

function scanTableForMen u()
{
$dbresult = mysql_query('SE LECT * FROM product_categor ies
WHERE product_categor ies_id != 0
');
$this->tmpArray = array();
while ($row = mysql_fetch_arr ay($dbresult))
{
$this->tmpArray[$row['product_catego ries_id']]['parent_id'] =
$row['product_catego ries_parent_id'];
$this->tmpArray[$row['product_catego ries_id']]['name'] =
$row['product_catego ries_name'];
$this->tmpArray[$row['product_catego ries_id']]['body'] =
$row['product_catego ries_body'];
$this->tmpArray[$row['product_catego ries_id']]['img'] =
$row['product_catego ries_img'];
}
$this->depth($this->tmpArray,0,0 );
echo "<ul>";
$this->displayMenu($t his->tree);
echo "</ul>";
}
function depth($tmpArray ,$parent_id,$le vel)
{
reset ($tmpArray);
foreach($tmpArr ay 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($tr ee,$currentLeve l = 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']>$currentLeve l)
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($t his->tree,$currentL evel+1);
}
else
{
for($i=0;$i<$cu rrentLevel;$i++ )
{
echo "</li>";
echo "</ul>";
}
$this->displayMenu($t his->tree,$currentL evel=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=ar ray();
$e1=array(1=>'P eter fox',2=>'Sally Fox',3=>'Geoffr ey Fox');
$e2=array(11=>' Teddy fox',12=>'Jean Fox');
$departments[1]=array('Sales', $e1);
$departments[10]=array('Product ion',$e2);
// set up tree and organise templates
$t = new menuTree();
$t->AddLinkTemplat e(1,'');
$t->AddLinkTemplat e(2,'department .php?DEPT=[DID]');
$t->AddLinkTemplat e(3,'employee.p hp?EMPNO=[EID]');

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

// populate tree
foreach($depart ments as $did=>$dept){
$departmentName =$dept[0];
$dnode = new treeNode($depar tmentName,array ('DID'=>$did));
$tnode->AddChild($dnod e);
$employeesOfDep t=$dept[1];
foreach($employ eesOfDept as $eid=>$employee Name){
$dnode->AddChild(new treeNode($emplo yeeName,array(' EID'=>$eid)));
}
unset($dnode);
}

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

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

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

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

Similar topics

4
8476
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 | 3 | 1 | SubMenu 1 of menu 1
4
5192
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 the menu content in the same part of the page where the menu goes (BODY). The content of the page is generated dynamically so I can't put the menu content it on the headers.
1
17673
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
2
1762
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 due to client requirement i have to avoid using frames and hence menu and the page will be a single page. So is there is any way by which the left hande side menu block can be cached someway so that dynamic generation can be avoided each time...
3
4958
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 is by hardcoding them into the database. I would like a way to sort them with drag-and-drop or with up/down arrows. Do you think that except PHP & MYsql I will need Javascript as well ?
4
19113
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 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...
3
4793
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 dynamically generated, and the dynamic generation needs to occur right when the user selects this menu item (that is on the Popup event handler). However, everytime I put following code on the Popup event handler (of the View Files menuitem) to dynamically...
1
2848
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 with it. So, on Redhat, we have two versions of PHP - Apache uses 4.3.9, there's also a 4.4.2 that is default from command-line. 4.3.9 was installed by RPM, 4.4.2 was built from source. I can get mysql.so loaded and working fine in 4.4.2, but...
3
4611
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 a database. My menu will be structured as follows: The first setion of the menu is hard-coded (the home and management sections). The rest of the menu will be populated from a database. The top level links (categories) will come from one table...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
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 we have to send another system
3
2890
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.