2

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 drop 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?

1
  • I'm not too familiar with PHP but you should be able to bind your dataset returned from the database with your drop down list control. Hope this helps! Commented May 17, 2011 at 15:53

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And if the value portion is non-numeric, better pass it through htmlspecialchars() so that any embedded quotes don't "break" the form.
1

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>

Comments

0
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.