Page 1 of 1
while() and <option>
Posted: Wed Oct 15, 2003 4:57 pm
by m3mn0n
Code: Select all
<?php
function printavataroption ($ammount,$type) {
global $users; // so i can grab avatar info for each user; not used yet
$t = $type; $i = 1; $y = $ammount;
while ($i <= $y){
echo "<option value="$t".$i.".gif">$t".$i.".gif</option>\n";
$i++;
}
}
?>
I have a directory filled with about 250 images. Instead of making an option tag for each image i instead renamed them into 4 catagories and numbered them from 1 to however many there is. The above function I made prints the <option>'s in the <select> in the <form> for me.
eg.
Code: Select all
<?php
printavataroption ("female","20");
printavataroption ("male","30");
?>
And this of course will send the value to the <option> to a database.
Now my question is how can I match the value in the database with one of the <option> tags being displayed so i can add the 'selected' value in the <option> tag?
This is got me playing around with various methods but none of them worked properly.

Posted: Wed Oct 15, 2003 5:11 pm
by Gen-ik
There a few ways of doing this.
This is probably better for a small amount of options.....
Code: Select all
<form>
<select>
<option value="1" <? if($var==1) echo "selected"; ?>OptionOne</option>
<option value="2" <? if($var==2) echo "selected"; ?>OptionTwo</option>
</select>
</form>
....... if you have loads of options to display then maybe use something like this..... which uses a PHP array.......
Code: Select all
<form>
<select>
<?
$options["OptionOne"]["1"]; // option text then its value
$options["OptionTwo"]["2"];
foreach($options as $Text => $Value)
{
if($var==$Value)
{
echo ('<option value="{$Value}" selected>{$Text}</option>\n');
}
else
{
echo ('<option value="{$Value}">{$Text}</option>\n');
}
}
Posted: Wed Oct 15, 2003 6:21 pm
by Stoneguard
Basically building on Gen, I would add an extra parm to the function, to pass in the selected value, then just match it from the list:
Code: Select all
<?php
function printavataroption ($ammount,$type, $sel) {
global $users; // so i can grab avatar info for each user; not used yet
$t = $type; $i = 1; $y = $ammount;
while ($i <= $y){
$val = $t .$i. ".gif";
$selected = ($val == $sel) ? " selected" : "" ;
echo "<option $selected value="" .$val. " ">" .$val. "</option>\n";
$i++;
}
}
?>
Posted: Wed Oct 15, 2003 6:27 pm
by JAM
Did you just read over my shoulder?! I posted the exactly same, so I deleted it. Annoying...
I did it by making the query inside the actual function, outside the while-loop tho. Just because the not yet used global $users could be used to store username or similiar needed for the database transaction.
Posted: Wed Oct 15, 2003 6:31 pm
by Stoneguard
Heh, yes I did think about that

. Sorry to {steal} your post

, didn't mean to.
Posted: Wed Oct 15, 2003 6:35 pm
by JAM
Steal? I sold it. Just wait untill the check arrives...
Oh well, just that it was the second time, today, it happened... How often does that happen. (McGruff last time tho)
Posted: Wed Oct 15, 2003 7:06 pm
by m3mn0n
Thank you guys! That is exactally what I needed.
Damn, I don't know why I didn't think of that.
