How do i set selected item in a dropdown PHP MySQL

Codes here !

Moderators: egami, macek, gesf

Post Reply
User avatar
New php-forum User
New php-forum User
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Sun Apr 29, 2012 9:56 am

With each while loop you receive one row to $rowBus (with each loop variable is overwritten with new value), so there is no need to use foreach loop.

In foreach loop you're taking current row index to $key and value to $value. Then compare current row 'bus_id' value to $value variable, which always return true, since they contain same value.
Why are you doing this? :?

Also, when you describe array index, always use quotes. So that PHP wouldn't search for defined constants to insert into index.
For short, use $rowBus['bus_no'] instead of $rowBus[bus_no]

Code: Select all

$SelectOptionWithThisID = 7;
$selBus=mysql_query("SELECT * FROM najdsy_bus order by bus_id");
while($rowBus = mysql_fetch_array($selBus)){
    if ($rowBus['bus_id'] == $SelectOptionWithThisID){
    } else {
    echo '<option value="'.$rowBus['bus_id'].'"'.$SelectedBus.'>'.$rowBus['bus_no'].' '.$SelectedBus.'</option>';

Post Reply