# PHP coding help select option box

**URL:** <https://www.phphelp.com/t/php-coding-help-select-option-box/24616>\
**Category:** General Chat\
**Created:** [October 6, 2015, 10:22am UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616 "2015-10-06T10:22:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adavison2](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@adavison2](https://www.phphelp.com/u/adavison2)\
**Post date:** [October 6, 2015, 10:22am UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616/1 "2015-10-06T10:22:45Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [October 6, 2015, 11:56am UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616/2 "2015-10-06T11:56:39Z")

</div>

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");}

---

<div class="post-metadata">

**Author:** ![adavison2](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@adavison2](https://www.phphelp.com/u/adavison2)\
**Post date:** [October 6, 2015, 12:28pm UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616/3 "2015-10-06T12:28:58Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![adavison2](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@adavison2](https://www.phphelp.com/u/adavison2)\
**Post date:** [October 6, 2015, 12:32pm UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616/4 "2015-10-06T12:32:30Z")

</div>

Thanks ErnieAlex

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [October 6, 2015, 4:22pm UTC](https://www.phphelp.com/t/php-coding-help-select-option-box/24616/5 "2015-10-06T16:22:17Z")

</div>

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]
