Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to use an if statement for select option help

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
What is wrong with this.
I am trying to put SELECTED FOR THE select option.
It is erroring on

if ($strSid == $mySid) {
echo "SELECTED";
}

Can anyone tell me what I am doing wrong.
<?php
$sql = "select * from tbl_status";
$result = mysql_query($sql)
or die("INvalid query: " . mysql_ERROR());
while ($row = mysql_fetch_array($result)){
$mySid = $row['s_id'] ;
echo '<option value="' . $row['s_id'] . '" ' . if ($strSid == $mySid) {
echo "SELECTED";
} . '>' . ucwords($row['desc']) . "</option>\r\n";
}
?>
 
You Seriously expected that to work? You can't concatenate an IF in a string? "If" is a control structure, not the result of a function or a variable that you can stick in a string.

You'll have to break your string into sections for that to work.


Code:
echo '<option value="'  . $row['s_id'] . '" '; 
if ($strSid == $mySid) {
echo "SELECTED";
}
echo "'>' .  ucwords($row['desc'])  . "</option>\r\n";




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the proper attribute for a selected option is
Code:
<option value="somevalue" [red]selected="selected"[/red]>some Label</option>

and i tend to do this to make life easier

Code:
<?php
$sql = "select * from tbl_status";
$result = mysql_query($sql)
or die("INvalid query: " . mysql_ERROR());
while ($row = mysql_fetch_assoc($result)){
  [red]$selected = $strSid == $row['s_id'] ? 'selected="selected" : '';[/red]
  echo <<<HTML
   <option value="{$row['s_id']}" [red]$selected[/red] >
HTML
   .  ucwords($row['desc'])  . "\r\n</option>\r\n";
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top