Saturday, January 5, 2013

Dropdown List in PHP

Introduction
A Dropdown list is a combination of items in a list. In the dropdown list menu only the title is visible but not the contents. The contents are shown only when the user activates it by a small arrow. The user may select an item from the menu by dragging the mouse from the menu title or by clicking the title and then clicking the item. The dropdown list box is read-only. We can say a drop down list is a box containing a list of multiple items. A drop-down list is a list in which the selected item is always visible, and the others are visible on demand by clicking a drop-down button.
The main advantages of a drop down list is to save space because the number of views is not limited by space. There are two types of dropdown lists, which are as follows:
  • Static dropdown list
  • Dynamic dropdown list
1. Static Dropdown list
Static dropdown list is used in the HTML form. Static means there are no database connectivity with the dropdown list.
An example of a static dropdown list is as follows:
Code
<
html>
<
head></head>
<
title>Static Dropdown List</title>
<
body bgcolor="pink">Employee List :<select>  <option value="Select">Select</option>}  <option value="Vineet">Vineet Saini</option>  <option value="Sumit">Sumit Sharma</option>  <option value="Dorilal">Dorilal Agarwal</option>  <option value="Omveer">Omveer Singh</option>  <option value="Rohtash">Rohtash Kumar</option>  <option value="Maneesh">Maneesh Tewatia</option>  <option value="Priyanka">Priyanka Sachan</option>  <option value="Neha">Neha Saini</option></select> </body><html>
Output
image3.jpg
2. Dynamic Dropdown list
Dynamic dropdown list means, choose items from a list at run time. Dynamic dropdown list is used in the PHP form. Dynamic means there is database connectivity available for the dropdown list.

Step by step solution to create dynamic dropdown list
Step 1. First of all we create a database in MySql using the following query.
           CREATE DATABASE `company`;
Step 2. After that then we create a table in the database. For create table in the database you can use the following query.
           CREATE TABLE `company`.`employee` (`emp_id` INT(10) NOT NULL AUTO_INCREMENT, `emp_name` VARCHAR(30) NOT NULL,
           PRIMARY KEY(`emp_id`)) ENGINE = InnoDB;

Step 3. Now we insert the values in the table using the following queries.
           insert into employee (emp_id,emp_name) values(0,'Vineet Saini');
 
           insert into employee (emp_id,emp_name) values(0,'Sumit Sharma');

           insert into employee (emp_id,emp_name) values(0,'Dorilal Agarwal');

           insert into employee (emp_id,emp_name) values(0,'Omveer Singh');

           insert into employee (emp_id,emp_name) values(0,'Maneesh Tewatia');

           insert into employee (emp_id,emp_name) values(0,'Rohtash Kumar');

           insert into employee (emp_id,emp_name) values(0,'Priyanka Sachan');

           insert into employee (emp_id,emp_name) values(0,'Neha Saini');

Step 4. When we execute the above query then you will get the following table.
image1.jpg
Step 5. Now we will use the above table in the dropdown list using the following code.
Code


    <head>    <title>Dynamic Drop Down List</title>    </head>    <BODY bgcolor ="pink">        <form id="form1" name="form1" method="post" action=" echo $PHP_SELF; ?>">            Employee List :
           
<select Emp Name='NEW'>            <option value="">--- Select ---</option>                            mysql_connect ("localhost","root","");
                mysql_select_db (
"company");
                $select=
"company";
               
if (isset ($select)&&$select!=""){
                $select=$_POST [
'NEW'];
            }
           
?>                            $list=mysql_query("select * from employee order by emp_id asc");
           
while($row_list=mysql_fetch_assoc($list)){
               
?>                    <option value=" echo $row_list['emp_id']; ?>" if($row_list['emp_id']==$select){ echo "selected"; } ?>>
                                        
echo $row_list['emp_name'];?>
                    </
option>
                                }
               
?>            </select>            <input type="submit" name="Submit" value="Select" />        </form>    </body>
Output
image2.jpg
Conclusion
So in this article you saw how to create a static dropdown list and dynamic dropdown list. Using this article one can easily understand how to create static and dynamic dropdown lists in PHP

No comments:

Post a Comment