Welcome to WebmasterWorld Guest from 50.16.31.61

Forum Moderators: coopster & jatar k

Populating Drop Down Boxes

   
5:59 pm on Feb 24, 2008 (gmt 0)



I have a number of drop down boxes that I want to populate dynamically when the page loads. What is the best approach to solve this problem?
9:31 pm on Feb 24, 2008 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Here is a model

<select name=\"search_city\">";
$conn=db_connect();// your DB conn function or hard coded DB conn script
// note: $state was passed via a POST as such it calls for mysql_real_escape_string()
$query = "SELECT DISTINCT `city` FROM my_table WHERE `state` = '".mysql_real_escape_string($state)."' ORDER BY `city` ASC";
$result= mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
// loop till all cities are gathered
// $i (for ittereation) will run untill $i <$num is no more correct
while ($i <$num)
{ // then build the options list
echo "<option>";
$search_city=mysql_result($result,$i,"city");
echo"$search_city";
$i++;
}
echo"</select>";

5:21 pm on Apr 22, 2008 (gmt 0)



Hi Henry,
Just getting back to this one....

What about a php scripts that contains the drop-down list. Each time the page loads, it calls the script to load the drop-down. This way, I do not have to populate a database....

Thanks...

 

Featured Threads

Hot Threads This Week

Hot Threads This Month