Jump to content


Photo

POPULATE A DROP DOWN MENU


  • Please log in to reply
2 replies to this topic

#1 jamesnkk

jamesnkk
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 27 March 2006 - 05:18 PM

Hi, can anyone show me, how can I populate data into the drop down menu.
Example I want to display all the customer name in the drop down menu, so when the user selected the customer name, I could also capture the cust id together with the customer name into another table.



#2 Levinax

Levinax
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 March 2006 - 06:35 PM

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.

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>";
}


#3 jamesnkk

jamesnkk
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 27 March 2006 - 08:28 PM

[!--quoteo(post=358982:date=Mar 28 2006, 02:35 AM:name=Levinax)--][div class=\'quotetop\']QUOTE(Levinax @ Mar 28 2006, 02:35 AM) View Post[/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.

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>";
}
[/quote]
Thank so much for your coding , I will try as I am using mysql and php





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users