Page 1 of 1

populating a dropdown menu

Posted: Thu Apr 28, 2005 9:34 pm
by soianyc
I would like to populate a drop down menu using data from a table. I have searched around here and have tried the code to fit my needs to no avail unfortunately. Any help is always appreciated. Here is what i have so far

Code: Select all

<select name="ud_tof">
<?php

while ($j < $menurow){

$data = mysql_fetch_array($menu,$j);
$item .= $data['name'];
echo '<option>'.$item.'</option>';
$j++;
}
?>
</select>
As you can see here, i would like to take the data value name from a database, and populate it in a dropdown menu. The send the result to a variable name ud_tof. I get no results the way it currently is.

Regards

-SOIAnyc

Posted: Thu Apr 28, 2005 9:39 pm
by SBro
Start with this (assuming $menu is your resource result)

Code: Select all

...
while ($row = mysql_fetch_assoc($menu)) {
    echo '<option>'.$row['name'].'</option>';
}
...

Posted: Thu Apr 28, 2005 10:10 pm
by soianyc
Awesome, thanks alot.

-soiaNYC

Posted: Tue Jun 14, 2005 10:08 am
by ianhull
I am just a beginner trying to learn this php

Can anyone help with populating a dropdown box
from my table and column name company and then posting the value of this
to another page?

Any help will be appreciated.

Thanks in advance

Posted: Tue Jun 14, 2005 10:11 am
by soianyc
do you have any code written down already??? If so please post.

Posted: Tue Jun 14, 2005 10:25 am
by ianhull
No nothing really, I am just looking for ideas so that I can get started.

I know how to use the sql to select from and where and things and I have a dbconnection script which I use as a include.

My idea is to populate a dropdown based on company name and then pass the company name to another page through a hidden form field so that I can select other things from the database relating to the company name.

Any help would be very much appreciated.

and thanks for the quick reply

Posted: Tue Jun 14, 2005 10:31 am
by LostOne
here is some to show if you have an item already selected....

<?php
echo "<select name='Product' size=1 id=Product>";
echo "<option selected>Select <strong>NEW</strong> Product Type";
while($row = mysql_fetch_array($results))
{
?>
<option value = "<?=$row['db_productName'];?>"
<?php
if($Product == $row["db_productName"])
echo("selected");?>
><?=$row['db_productName'].' ($ '.$row['db_productPrice'].')'; ?></option>
<?

Posted: Wed Jun 15, 2005 2:47 am
by CoderGoblin
LostOne, sorry to say I find that code unreadable...

Would be much better as :

Code: Select all

<?php
  $select_str="";
  $select_str.="<select name=\"Product\" size=1 id=\"Product\">\n";
  $select_str.="<option>Select NEW Product Type</option>\n";
  while($row = mysql_fetch_assoc($results)) {
    $select_str.="<option value=\"".$row['db_productName'];
    if ($product == $row['db_productname']) $select_str.=" selected";
    $select_str.='>'.$row['db_productName'].' ($ '.$row['db_productPrice'].')</option>\n';
  }
  echo($select_str);
?>
I find this much more readable as you do not keep switching PHP on and off. Also be aware of the <strong> tag (Better is to use span with CSS). I believe the <Strong> it is only for Explorer (Also check that tags in select options work as I don't believe they do).

For an even more complicated example have a look at Javascript and Dynamic Select Boxes Which populates one select box when a user selects an option from the first.

Posted: Wed Jun 15, 2005 3:29 am
by LostOne
No worries, it is used in the middle of an HTML form. It will ask for a new product and list those products in a drop down and echo the selected option once chosen. It works like a charm. Thanks for the link!