Many of us are familiar with the HTML drop down menu which allows us select a choice within a list of options. A typical example for Male and Female is written as

<select name=”select”>

<option value=”m”>Male</option>

<option value=”f”>Female</option>

</select>

However, using this input during Form filling poses no threat until when you (as a developer) want to call/retrieve the users profile from the database with the chosen option in the List Menu. Well this is one thing HTML will sure not help you with. Server side scripting does a lot of its “magic” between the Browser, Web Server and Database. Using PHP, I have seen in some blogs and site where the HTML List Menu was tweaked to generate the desired result like;

<?php

$sql=mysql_query(“select gender from profile where user=’$_POST[‘user’]’”);

$row=mysql_fetch_array($sql);

echo “<select name=\”select\”>”;

If (isset($row[‘user’]))    {

…..

While this gets the job done, Imagine when you have a form with five (5) list Menus, you have a page swamped with series of 10 lines just for one purpose. My solution, get the list menu into a function and call with a function call. The function (select_field) below uses 5 variable fields.

/*// function to display a list menu with db values
use: function contains 5 parameters
1=> table name;
2=> name of list menu
3=>field name to select/display from db  e.g gender
4=>[Optional] default selected value for the list menu e.g $_POST[‘gender’]
5=>[Otional] Style Class for the list menu*/

function select_field($table, $name, $field, $known_value, $class)    {

$query=mysql_query(“SELECT $field FROM $table”) or die(“Invalid Field Select Query”. mysql_error());
echo “<select name=$name id=$name class=$class>”;
//echo “<option>- choose $name – </option>”;
while ($rows=mysql_fetch_assoc($query)) {
if ( $rows[$field]==$known_value ) {
echo “<option value='”. $rows[$field] .”‘ selected=SELECTED>” . $rows[$field] .”</option>”;br();
}
else    {
echo “<option value='”. $rows[$field] .”‘>” . $rows[$field] .”</option>”;br();
}
}
echo “</select>”;
}

The br(); is a little function for the HTML <br /> (Don’t ask why I have that also in a function?)
function br() {
echo ‘<br />’;
}

Using the Function
Using the gender field in the Profile Management example shwon above where
Table Name=Profile
ListMenu name=gender
Table Field in DB = user_gender

<?php
## <remember to include your mysql database connection here >
##Include the function file where the function is
require_once(‘drop-menu.php’);
?>

<form id=”profile” name=”profile” method=”POST” action=”submitform.php”>
<h1>Profile Management </h1><br />
<p>
<?php
##Simply Call the function line within the form
select_field(profile, gender,user_gender, $row [‘user_gender’], “”);
?>

</p>
<p>
<input type=”submit” name=”Submit” value=”Submit” />
</p>
</form>

Note: This function works also for fresh forms, It is loaded with all the field values from the DB table if fresh and here a ($_POST[‘variable’]) can be used to display what’s sent to the browser and on form edit or update, a ($row[‘variable’]) can be used to retrieve the DB value.

If you are very familiar with HTML, you can edit the function to display twin data in the select field like (Nigeria – 234), you can also add an extra variable to display an id for the List Menu instead of the Field Name, Least to say the possibilities with data are endless. With this, I don’t care how many Menu’s I have to use, One Line does it all.