Jump to content

Populating a select box from a database


cavey5

Recommended Posts

I have a subscription database that allows you to edit a subscribers data. For example, you enter a search term like "Bob" and it returns a list of everyone named Bob. You click the one you want and it goes to a subscriber detail page, where all of his data is populated to a form. You can directly edit this form and click save to overwrite it. The only issue is the select boxes (drop downs) - say I have four options in the drop down box, red, blue, green and black. On my details page for bob I end up with five, I have those four PLUS whatever was saved for his color, so it shows up twice. If Bob is green, my select box looks like:

 

Green

Red

Blue

Green

Black

 

How can I make it so that Green only shows up once?

Link to comment
https://forums.phpfreaks.com/topic/52934-populating-a-select-box-from-a-database/
Share on other sites

<select name="colors"><?php
   $result = mysql_query('QUERY');
   $row = mysql_fetch_assoc($result);

   $arr = array('Red', 'Blue', 'Green', 'Black');
   foreach($arr as $s) {
      if (strtolower($s) != $row['color'])
         echo '<option value="'.$s.'">'.$s.'</option>';
   }
?></select>

 

That doesn't seem to work because i am already inside this:

 

while ($row = mysql_fetch_array($findsubscriber_result)) {
	  $cm_company = $row["cm_company"];
	  $cm_phone = $row["cm_phone"];
	  $cm_email = $row["cm_email"];
                  etc. etc.

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.