Jump to content

[SOLVED] dynamic list population


lc21

Recommended Posts

Hi, sorry for a novice question but in PHP how do you populate a list dynamically? for example I have the following in a table but I can't get them into the list after the query is performed.

 

 

<form action="">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>

Link to comment
Share on other sites

if you have these items listed in a database table then this would work...

 

<pre>

id        name

-------------

1        Volvo

2        Saab

3        Fiat

4        Audi

 

<?php

$qry = "SELECT * FROM `cars`";
$qry = mysql_query($qry);

if (mysql_num_rows($qry) > 0)
{
?>
<select name="cars">
<?php
while($row = mysql_fetch_assoc($qry))
{
?>
  <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<?php

...rest of script....
?>

Link to comment
Share on other sites

Where is a function is use to do this

 

Pass the table use wish to query and the field that holds that data

 

Function Queryselectdisplay($tablename,$fieldname) {

$QSdist = "select Distinct ".$Fieldname." from ".$tablename." ORDER by ".$fieldname." ASC";

echo "<select name=".$fieldname.">";

echo "<option echo value=''>Make Selection</option>";

$options=mysql_query($QSdist);

  while($data = mysql_fetch_array($options)) {

echo "<option value='".$data[$fieldname]."'>".$data[$feildname]."</option>";

}

echo "</select>";

}

Link to comment
Share on other sites

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.