HI
i am having bit of trouble with i small bit of code see the select box works fine till i want it to select option it pulls from data base
so i have a query that make
$classname = 1C
the drop box is populated from another table
0c 1c 2c 3c 4c
what i want the code to do is to have 1C selected or what ever value is assign to variable $classname
hope this makes sense
Does not work
[php]<?php
$query=mysql_query(“SELECT Class FROM Class”);
while ($fetch=mysql_fetch_assoc($query)) {
echo ‘<option value="’.$fetch[‘Class’].’";
if($fetch[‘Class’] == $classname){echo(“selected”);}
echo >’.$fetch[‘Class’].’’;
}
?>[/php]
Works but always selects first option
[php]<?php
$query=mysql_query(“SELECT Class FROM Class”);
while ($fetch=mysql_fetch_assoc($query)) {
echo ‘’.$fetch[‘Class’].’’;
}
?>[/php]
The first way should work…
Well, you are missing an ending quote. (Obvious if you noted the color changes in your display.)
echo ‘<option value="’.$fetch[‘Class’].’";
Should be
echo ‘<option value="’.$fetch[‘Class’].’"’;
Also, you should echo a space before the “selected” as this sometimes causes your text to run on with
this option…
if($fetch[‘Class’] == $classname){echo(" selected");}
Not to worry I think I have sorted it
[php]$query=mysql_query(“SELECT Class FROM Class”);
while ($fetch=mysql_fetch_assoc($query)) {
if($fetch[“Class”] == $classname){$sel = “selected”;} else {$sel = “”;}
echo '<option value="'.$fetch['Class'].'";
echo ‘.$sel.’>’.$fetch[‘Class’].’’;
}[/php]
You have a mistake with your quotes. I cleaned up your code. Additionally, you should be using the record id for the value.
[php]<?php
$query = mysql_query(“SELECT Class FROM Class”);
while ($fetch = mysql_fetch_assoc($query))
{
$sel = $fetch[“Class”] == $classname ? ‘selected’ : ‘’;
echo “<option value=”{$fetch[‘Class’]}" $sel>{$fetch[‘Class’]}";
}
?>[/php]