开发者

Display data from SQL database in a drop down menu

I have a database with names stored. I have my query of the database working, but lets say I have 5 names that I want to display in a drop down menu. How do I make the default text in the drop down menu display those 5 names?

Basically what I am trying to accomplish is this:

Query my database and store all the names of clients to a variable. Say there are 5 names in the database, I need those 5 names to be stored in a variable. And then for my d开发者_StackOverflow中文版rop down menu, normally I put the text in like this: < option>Single Floor< /option>

But how do I get those 5 names to appear in the drop down list?


Below is a simple pseudo-script that selects information from a database and outputs a select drop down box. You will need to replace *_fetch_array with whatever DB Extension you are using, and $row['Value'] and $row['DisplayValue'] with the appropriate field names from your DB Schema.

<select name = 'iAmASelect' id = 'iAmaASelect'>
<?php
    $DB_Rows = /* fetch data from database */;
    while($row = *_fetch_array($DB_Rows))
        echo("<option value = '" . $row['Value'] . "'>" . $row['DisplayValue'] . "</option>");
?>
</select>

The select will submit $row['Value'] to the form handler, while displaying $row['DisplayValue'] to the user in the drop down list.


If I understand it right you want a select box where the first option contains all the names AND each name also is an option.

Either implode your array with names into a string.

or use a for(each) loop.

$string ='';
foreach($rows as $k=>$names ){

  $string.=$names.' ';

}
 $string =trim($string).

HTML :

<select>
  <option value='0' selected="selected"> <?=$string;?> </option>
  ## loop your names as options.
</select>


Just using the following code :

<select name="shipDec" id="shipDec">
     <?php
     for($i=0;$i<=200;$i++)
     {
      echo "<option value='".$i."'";
      if($shipDec==$i)
      {
      echo 'selected="selected"';   
      }
      echo ">".$i."</option>";  
     }

     ?> 
        </select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜