How to create Dynamic Menu using php and mysqli - Mostlikers

15 August, 2013

How to create Dynamic Menu using php and mysqli

This post concept on how to Make dynamic CSS menu or Auto generate menu. its nothing but normally make CSS menu concept just fetch menu name to the database using php and mysqli query. this concept most using on most of the admin panel websites and WordPress websites .



Crate below table field on database its connect query using in mysqli
   CREATE TABLE `menu` (
     `id` int( 11 )NOT NULL AUTO_INCREMENT ,
     `menu_name` VARCHAR( 25 ) NOT NULL ,
    )

PHP

<?php
if(isset($_POST['submit']))
{
$menu=$_POST['menu'];
$insert=$db->query("insert into menu(menu_name) values('$menu')");

if($insert)

{
echo "<script>alert('Menu upload sucessful')</script>";
}
else
{
die($insert->error);
}
}

?>

HTML

<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
.box
{
margin:0 auto;
border:dashed 2px #00CCFF;
width:400px;
}
.meanu li { display:inline;
list-style:none;
color:#666;
border:dashed 1px #00CCFF;
padding:5px 5px 5px 5px; }
}
</style>
</head>
<body>
<form name="menu-form" class="box" method="post">
<label>Add new Menu Name</label>
<input type="text" name="menu" title="menu" />
<p align="center"> <input type="submit" name="submit" value="submit" /></p>
</form>
<p>&nbsp;</p>
<ul class="meanu">
<?php
$sql=$db->query("SELECT * FROM `menu`");
while($page=mysqli_fetch_object($sql))
{
?>
<li><?php echo $page->menu_name; ?></li>
<?php } ?>
</ul>
</body>
</html>

2 comments:

  1. If I want create a menu with a submenu how can I do?

    ReplyDelete
    Replies
    1. create new sub menu table the id matched on main menu table. using if statement matching main menu id into sub id

      Delete