HTML ‘n’ Level Menu

Generating  HTML UL and LI ‘ ‘n’ level menu  in php can done as follows,

<?php
$stack = array();

$mysql = array();
$mysql[‘host’] = ‘localhost’;
$mysq[‘username’] = ‘username’;
$mysql[‘password’] = ‘password’;
$mysql[‘database’] = ‘dbname’;
$mysql[‘tbl_prefix’] = ”;

if(!$conn = mysql_pconnect($mysql[‘host’], $mysq[‘username’], $mysql[‘password’]))
{
echo “Error: Failed to connect MySQL”;
exit;
}
mysql_select_db( $mysql[‘database’], $conn);

$query=mysql_query(“select * from menus where parent_id=0 and active=1”);
echo “<div id=’myjquerymenu’ class=’jquerycssmenu’>”;
echo “<ul>”;
while($row = mysql_fetch_array($query))
{
//echo “<div id=’menu”.$row[‘id’].”‘ class=’dropmenudiv’>”;
$menuspath=”;
$arr = get_path($row[‘id’]);
for($j=0;$j<(sizeof($arr)-1);$j++)
{
$menuspath .= “/”.$arr[$j];
}
echo “<li class=’menu”.$row[‘id’].”‘><a href='”.$menuspath.”/”.$row[‘url’].”‘>”.$row[‘name’].”</a>”;
test($row[‘id’]);
//echo “</div>”;
echo “</li>”;
}
echo “</ul>”;
echo “</div>”;

function test($id)
{

$query1=mysql_query(“select * from menus where parent_id='”.$id.”‘ and active=1″);
$query3=mysql_fetch_array(mysql_query(“select * from menus where id='”.$id.”‘ and active=1″));
$parentmenuname = ”;
if(mysql_num_rows($query1)!=0)
{
//echo “<ul id=’verticalmenu’ class=’glossymenu’>”;
echo “<ul style=’border:1px solid #BDBDBC;’>”;
while($row1 = mysql_fetch_array($query1))
{

$menuspath=”;
$query4=mysql_fetch_array(mysql_query(“select * from menus where id='”.$row1[‘id’].”‘ and active=1″));
if($query4[‘parent_id’]!=0){

$arr = get_path($row1[‘id’]);
for($j=0;$j<(sizeof($arr)-1);$j++)
{
$menuspath .= “/”.$arr[$j];
}
}
echo “<li><a href='”.$menuspath.”/”.$row1[‘url’].”‘>”.$row1[‘name’].”</a>”;
$query2=mysql_query(“select * from menus where parent_id='”.$row1[‘id’].”‘”);

if(mysql_num_rows($query2)!=0)
{
//rootmenu($row1[‘id’]);

test($row1[‘id’]);
}
else
{
echo “</li>”;
}

}
echo “</ul>”;
}

}

function get_path($node) {
// look up the parent of this node
$result = mysql_query(‘SELECT parent_id,name FROM menus ‘.
‘WHERE id=”‘.$node.'”;’);
$row = mysql_fetch_array($result);

// save the path in this array
$path = array();

// only continue if this $node isn’t the root node
// (that’s the node with no parent)
if ($row[‘parent_id’]!=”) {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row[‘name’];

// we should add the path to the parent of this node
// to the path
$path = array_merge(get_path($row[‘parent_id’]), $path);
}

// return the path
return $path;
}
?>

The above code read the menu title from the menu tables to structure the UL and LI navigation as per our need. The Data Structure of the menu table will look like below,

DROP TABLE IF EXISTS `menus`;

CREATE TABLE `menus` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) default NULL,
`admin_id` int(10) default NULL,
`parent_id` int(10) default NULL,
`url` varchar(255) default NULL,
`order` int(10) default NULL,
`active` int(10) default NULL,
`default_item_id` int(10) default ‘0’,
`created` datetime default NULL,
`modified` datetime default NULL,
`created_from` int(10) NOT NULL default ‘0’,
`ismodifiedby` varchar(256) default NULL,
`iscreatedby` varchar(256) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=96 DEFAULT CHARSET=latin1;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: