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
https://forums.phpfreaks.com/topic/75298-solved-select-box-from-array/
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

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?

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

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.