populating a dropdown menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

populating a dropdown menu

Post 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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post 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>';
}
...
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

Awesome, thanks alot.

-soiaNYC
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

do you have any code written down already??? If so please post.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

Post 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>
<?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

Post 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!
Post Reply