Forum Moderators: coopster

Message Too Old, No Replies

Set selected option of select tag & fetch array

         

basketmen

2:43 pm on Nov 19, 2014 (gmt 0)

10+ Year Member



I have this code, there is no selected yet
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' ">$datas[product]</option>";
}




I want to set selected option, already tried this in red, but its selected all value
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . selected . ">$datas[product]</option>";
}





then tried this in red
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . if ($datas['product'] == 'test') { echo 'selected' . ">$datas[product]</option>";
}

the result is getting error message like this, maybe its still not correct or cant use php code inside it :
Parse error: syntax error, unexpected T_IF in /home/username/public_html/test.php on line 29




tried put echo, just for test
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . echo 'test'; . ">$datas[product]</option>";
}


the result is getting error message like this, so event cant print simple echo inside it :
Parse error: syntax error, unexpected T_ECHO in /home/username/public_html/test.php on line 29



please help share your knowledge guys, what is the right code to set selected in above code

GBU for all the answering

Demaestro

5:03 pm on Nov 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The way you have it formed you want this...


while($datas=mysql_fetch_array($rs)){
$selected = ($datas['product'] == "test" ? "selected='selected'" : "" );
$data .= "<option value='" . $datas[product] . "' " . $selected . ">" . $datas[product] . "</option>";
}

basketmen

1:15 am on Nov 20, 2014 (gmt 0)

10+ Year Member



thank you, gbu