Menu

Dynamic Drop-Down List PHP MySql tutorial

PHP By May 27, 2014 No Comments


Dynamic Dropdown list PHP

Hello friends i have noticed that many times we need a dynamic dropdown list in php. So i think to help those who are new to this so here i am writing the Dynamic Drop Down List PHP MySql Tutorial. Hope you will like it lets start …..

Things we need

  • Database
  • PHP Script
  • Page to show the list

Suppose we have a database and a table named “EmployeeDetails” with 4 columns (Id, Name, Dob, Mobile). We will take the “Name column” in our Dropdown to make our Dropdown dynamic or based on database. Here is the php script to do it.

//$this->con is the database connection variable
    $responce=new messages();
    $value="select * from EmployeeDetails";
    $this->result=  mysqli_query($this->con, $value);
    $this->count=  mysqli_num_rows($this->result);
    if($this->count < 1)
    {
        
    }
    else
    {
        $alert.="<select id='employeename' name='employeename'><option value=employee>Select Employee</option>";
        while($row=  mysqli_fetch_array($this->result))
        {
            $alert.="<option value='$row[Id]'>$row[Name]</option>";
        }
        $alert.="</select>";
        return $alert;
    }

Above script will pop up the Dynamic Dropdown List. For using it create a function enter this script and than call that function whenever you need it.

Why we use option value =$row[ID] ?

It is because suppose you realise that 1 employee name spell is incorrect now you edited that but somewhere in your application you used this dropdown and saved the name of that employee so it will not update that spell. For this type of problem we use the ID only so that you do whatever you want to do with the name it will change everywhere in your application.

Hope you like the Dynamic Dropdown List PHP Mysql tutorial. If you find any issue with the code or you want to consult or you have a better idea to it than please share it with us so that our subscribers will get the benefit. Thank You please comment and let me know about the tutorial.

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *