Jump to content

[SOLVED] select box from array


eagleweb

Recommended Posts

I have a field in a db called 'option1' in which are three comma and space seperated colors; red, green, blue

The person filling out the form has to choose a color. I can't figure out how to make the three colors appear in a drop down menu.

$query3 = mysql_query("SELECT * FROM products WHERE prodlineid = '".$row2['id']."' ORDER BY ordered", $conn);
$row3 = mysql_fetch_assoc($query3);
$opt1 = $row3['option1'];

if you echo $opt1 you will get red, green, blue

I have exploded and imploded and can get various lists, but have not figured out how to get them into a select field in a form.

<?php do { ?>
   <option value="<?php echo $opt1; ?>"><?php echo $opt1; ?></option>
<?php										
  } while ($row3 = mysql_fetch_assoc($query3));
															?>

Link to comment
Share on other sites

.

.

.

$opt1 = $row3['option1'];

$arrOptions = explode(',', $opt1);

// echo select part

for ($i = 0, $intCnt = count($arrOptions); $i < $intCnt; $i++) {

    $strOption = trim($arrOptions[$i]);

    echo "<option value=\"$strOption\">$strOption</option>";

}

// echo /select part

Link to comment
Share on other sites

Here is what I have so far, but I am getting an error.

<?php
$opt1 = $row3['option1'];
$arrOptions = explode(', ', $opt1);
echo "<select name='colors'>";
for ($i = 0, $intCnt = count($arrOptions); $i < $intCnt) {
$strOption = trim($arrOptions[$i]);
echo "<option value=\"$strOption\">$strOption</option>";
} // end for
echo "</select>";
?>

The page is giving me the error: Parse error: parse error, unexpected ')', expecting ';'

and it is on the line that starts the 'for' stmnt.

What is not right there?

Link to comment
Share on other sites

I hope this helps someone. It was simpler than I thought. I was making it too hard:

$opt1 = $row3['option1'];
$arrOptions = explode(', ', $opt1);
$intCnt = count($arrOptions);
echo "<select name='colors'>";
for ($i = 0; $i < $intCnt; $i++) {
$strOption = trim($arrOptions[$i]);
echo "<option value=\"$strOption\">$strOption</option>";
} // end for

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.