Populating dropdown box from array and foreach loop in PHP

Hear we shared simple coding a populating dropdown box from array and foreach loop. Using this code you can reduce the size of drop down list code.

<?php
//This tutorial will explain how to populate records to dropdown box using array function and foreach loop. Lets start....

//First create all the array values in array variable
$arr = array("India","Sri lanka", "Canada", "Pakistan", "Bangla desh","USA","Candada", "Nepal");

//Now i create select option dropdown tag
echo "<select name='country' >";
echo "<option value=''>Select</option";
//Now i will create foreach loop statement.
foreach($arr as $val)
{
//Here I will display all the records from $arr variable inside the dropdown list
echo "<option value='$val'>$val</option>"; 
}
//Here i will close the select tag
echo "</select>";
//end of this code.
?>

 Download source code>>

🔥755 Views

Anu

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.