parse error in php for the statement syntax, missing something
I'm trying to echo the value of the field form the database in the drop-down list retrieved from another table in the database, but I keep getting the Parse error: syntax error, unexpected T_IF
I know i am m missing something in the brackets to comma, but I can't seem to see it,
<?php
$result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
foreach($row1 AS $key1 => $value1) {
$row1[$key1] = stripslashes($value1);
}
echo "<option value=" . nl2br( $row1['CompanyID']) . " ". if($row['CompanyID'] == $Merchant) echo 'selected = "selected"'
. ">" . nl2br( $row1['Name']) . "</option>";
}
?>
你可以这样做:
echo 'something ' . ( true ? 'statement is true' : 'statement is false') . ' continue echo';
You can't use if
like you did in your code (in a string). It returns nothing. Use this:
$sValue = boolean_value ? 'value if TRUE' : 'value if FALSE';
It's called a "Ternary Operator". PHP docs link:
http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Change it like this, for example:
<?php
$result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
foreach($row1 AS $key1 => $value1) {
$row1[$key1] = stripslashes($value1);
}
$sSel = ($row['CompanyID'] == $Merchant) ? 'selected = "selected"' : '';
echo "<option value=" . nl2br( $row1['CompanyID']) . " ".sSel.">" . nl2br( $row1['Name']) . "</option>";
}
?>
You can also use if
, if You want, but in other way:
if($row['CompanyID'] == $Merchant) {
$sSel = 'selected = "selected"';
} else {
$sSel = '';
}
But I surely prefer 1st solution which I presented to You.
All the mistakes are removed.
Corrected Code:
<?php
$result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
foreach ($row1 as $key1 => $value1) {
$CompanyID = stripslashes($value1);
$CompanyID = nl2br($CompanyID);
$selected = ($CompanyID == $Merchant) ? 'selected = "selected"' : '';
echo '<option value="' . nl2br( $CompanyID) . '"' . $selected . '></option>';
}
}
?>
链接地址: http://www.djcxy.com/p/69818.html