adding elements to an array in a loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

adding elements to an array in a loop?

Post by psychomachine »

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:

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

Code: Select all

$whichselected = mysql_result ($q)

for($n=0; $n<=3; $n++) &#123;
if $whichselected = $n &#123;
$number = "'".$n ."'". "selected";
&#125; else &#123;
$number = "'".$n."'";
&#125;
&#125;
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:

Code: Select all

echo "<option value=". $array&#1111;0] . "Select from the menu</option>"
echo "<option value=". $array&#1111;1].  "some text" etc...
finally, if all this is really ugly, what would be a more elegant way?

all best,

pschmchn
Morpheus
Forum Newbie
Posts: 6
Joined: Thu Mar 11, 2004 7:00 am
Location: South Africa

Post by Morpheus »

Hi, ok... i think i've got a bit of a more efficient and user (developer) friendly way for you to do this:

First, let me make an assumption that the query will return for instance 0, 1 and 3 meaning that option 0, 1 and 3 is selected, right? This means that there's three rows in the result set of this query, one with "0" in it, one with "1", one with "3", right? If so, here's what I propose:

Code: Select all

$whichselected = mysql_query("SELECT selectedvalue, name FROM your_table");
// <<< this part is fine... as long as it returns only one element, say for instance the element returned is "selectedvalue"

$selectedARR = array (); // you don't really have to do this, but i'm old school  
while ($Result = mysql_fetch_array($whichselected, MYSQL_NUM)) {
   $selectedARR[] = $Result[0]; // this gives the next element in selectedARR the value 1,2 or 3 (for this example) respectively
} // end while

// Now you have an array with three elements in it (for this example), first element holds "0", second holds "1", third holds "3"

// Using your code (slightly revised) here:
echo "<option value=0".(in_array (0, $selectedARR) ? " selected" : "").">Select from the menu</option>";
echo "<option value=1".(in_array (1, $selectedARR) ? " selected" : "").">some text" etc...
Hope this helps. This code you can re-use aswell ;)
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

Post by psychomachine »

thanks morpheus. i think that's it. i have to now actually go through it slowly (i am VERY slow :) but i think this should do it.

all best,

pschmchn
Morpheus
Forum Newbie
Posts: 6
Joined: Thu Mar 11, 2004 7:00 am
Location: South Africa

Post by Morpheus »

Cool. Hope it works right... you can mail me if you run into trouble ;)
Post Reply