Welcome to WebmasterWorld Guest from 184.72.177.182

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Populating Drop Down Boxes

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

Junior Member

joined:Sept 3, 2007
posts: 59
votes: 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)

Senior Member from US 

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

joined:Apr 19, 2003
posts: 4388
votes: 2


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)

Junior Member

joined:Sept 3, 2007
posts: 59
votes: 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...

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members