while() and <option>

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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

while() and <option>

Post 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. :x
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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');
    }
}
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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++; 
   } 
} 
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Heh, yes I did think about that ;). Sorry to {steal} your post :oops:, didn't mean to.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Thank you guys! That is exactally what I needed.

Damn, I don't know why I didn't think of that. ;)
Post Reply