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
https://forums.phpfreaks.com/topic/60202-solved-dynamic-list-population/
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....
?>

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

}

Archived

This topic is now archived and is closed to further replies.

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