Jump to content

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.