Jump to content

POPULATE A DROP DOWN MENU


jamesnkk

Recommended Posts

here is a function i wrote to query a database and populate a SELECT box based on the results of that query if thats what you're looking for. there are probably better ways of doing it, but i know this one works.

one thing i should note, this POSTS the information, and keeps whatever you selected selected. you might want to modify it if thats not what you want to do. and i was using PostgreSQL, so you might need to change some things from there too.

[code]function first_form($db, &$location){

//Getting the data from Postgres table for first list box
$quer1="SELECT DISTINCT location FROM info";
$result1 = pg_query($db, $quer1);
$rows1 = pg_num_rows($result1);
$location = $_POST["loc"];

//creat first form
echo "<form method=POST name=f1 action=$PHP_SELF>";
echo "<select name=loc onchange=\"Javascript:submit()\"><option value=\"\">Location</option>";


for($i = 0; $i < $rows1; $i++)
{
        $results1 = pg_fetch_array($result1, NULL, PGSQL_ASSOC);
        if($results1[location] == $location)
        {
                echo "<option selected value=$location>$location</option>";
        }
        else
        {
                echo "<option value=\"$results1[location]\">$results1[location]</option>";
        }
}

echo "</select>";
}
[/code]
[!--quoteo(post=358982:date=Mar 28 2006, 02:35 AM:name=Levinax)--][div class=\'quotetop\']QUOTE(Levinax @ Mar 28 2006, 02:35 AM) [snapback]358982[/snapback][/div][div class=\'quotemain\'][!--quotec--]
here is a function i wrote to query a database and populate a SELECT box based on the results of that query if thats what you're looking for. there are probably better ways of doing it, but i know this one works.

one thing i should note, this POSTS the information, and keeps whatever you selected selected. you might want to modify it if thats not what you want to do. and i was using PostgreSQL, so you might need to change some things from there too.

[code]function first_form($db, &$location){

//Getting the data from Postgres table for first list box
$quer1="SELECT DISTINCT location FROM info";
$result1 = pg_query($db, $quer1);
$rows1 = pg_num_rows($result1);
$location = $_POST["loc"];

//creat first form
echo "<form method=POST name=f1 action=$PHP_SELF>";
echo "<select name=loc onchange=\"Javascript:submit()\"><option value=\"\">Location</option>";
for($i = 0; $i < $rows1; $i++)
{
        $results1 = pg_fetch_array($result1, NULL, PGSQL_ASSOC);
        if($results1[location] == $location)
        {
                echo "<option selected value=$location>$location</option>";
        }
        else
        {
                echo "<option value=\"$results1[location]\">$results1[location]</option>";
        }
}

echo "</select>";
}
[/code]
[/quote]
Thank so much for your coding , I will try as I am using mysql and php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.