adding elements to an array in a loop?
Posted: Thu Mar 11, 2004 6:56 am
I look up a value in the db and use it to determine which element in the dropdown menu should be selected.
The dropdown looks like this:
values 1, 2 and 3 actually correspond to the values i'm looking up in the db. if no value is already in the database, 0 should be selected.
SOOOOO, I got this far: checking the number in the database, going through numbers 0-3 and adding "selected" to the number which corresponds to the num in the db.
but how can i now put each $number in a numeric array so that I can use it to generate the dropdown menu, along the lines of:
finally, if all this is really ugly, what would be a more elegant way?
all best,
pschmchn
The dropdown looks like this:
Code: Select all
<option value = "0">Select from the menu</option>
<option value = "1">some text</option>
<option value = "2">some text </option>
<option value = "3">some text </option>SOOOOO, I got this far: checking the number in the database, going through numbers 0-3 and adding "selected" to the number which corresponds to the num in the db.
Code: Select all
$whichselected = mysql_result ($q)
for($n=0; $n<=3; $n++) {
if $whichselected = $n {
$number = "'".$n ."'". "selected";
} else {
$number = "'".$n."'";
}
}Code: Select all
echo "<option value=". $arrayї0] . "Select from the menu</option>"
echo "<option value=". $arrayї1]. "some text" etc...all best,
pschmchn