PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | Php vs Node.Js | Web Tools

Dynamic Dropdown Menu


Tutorials > Dynamic Dropdown Menu

Put records from your database in a drop down menu/list box. You can apply it as a navigator list box. Good for saving your webpage areas or you have many links you don't want to show all of them in your pages.

This tutorial require 1 PHP file and 1 table of mySQL database.

  1. dynamic_menu.php
  2. Database "tutorial" and table "name_list" with 2 fields: id(auto_increment), name(varchar, 50) and put some records about 20 - 30 records into this table. (directly by phpMyAdmin)

Dynamic Menu

dynamic_menu.php

Source Code
<?
// Connect database
mysql_connect("localhost","","");
mysql_select_db("tutorial");

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&&$select!=""){
$select=$_GET['select'];
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="get" action="<? echo $PHP_SELF; ?> ">
Car company :
<select name="select">
<option value="">--- Select ---</option>
<?
// Get records from database (table "name_list").
$list=mysql_query("select * from name_list order by id asc");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>

<option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['name']; ?></option>
<?
// End while loop.
}
?>

</select>
<input type="submit" name="Submit" value="Select" />
</form>
<hr>
<p>
<?
// If you have selected from list box.
if(isset($select)&&$select!=""){

// Get records from database (table "name_list").
$result=mysql_query("select * from name_list where id='$select'");
$row=mysql_fetch_assoc($result);
?>

Information about <strong><? echo $row['name']; ?></strong> company...</p>
<p>........................................<br>
........................................<br>
........................................
<?
// End if statement.
}

// Close database connection.
mysql_close();
?>

</p>
</body>
</html>

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
[email protected]