尝试添加两个php命令时出现语法错误

时间:2014-01-01 14:02:25

标签: php if-statement syntax

我想在这一行添加:

echo '<tr><td>'.$id.'</td><td><a href="motor.php?TYPE='.$tip.'">'.$TYPE.'</a></td><td>'.$Power.' kW</td><td>'.$Speed.'</td><td>'.$Weight.' кг.</td><td>'.$info.'</td></tr>';

而不是$ tip一些if-else语句:

if ($arr[0]<='K2251S') $tip=$arr[0] ; elseif($arr[0]<='K2251S' && $Poles=2) $tip=$arr[0].'-2';

但我得到Parse error: syntax error, unexpected T_IF in /home/eldvigat/public_html/MotorSelect/list.php on line 86

编辑完整代码:

<form action="/MotorSelect/list.php" method="get">

<?php
// moshtnost avto
$sql = "SELECT DISTINCT Power FROM `TABLE 2` ORDER BY id";
$result = mysql_query($sql) or die(mysql_error()."[".$sql."]");
?>

<select name="Power" id="Power" onchange="this.form.submit()">
<option value="*">Show all available</option>
<?php 
while ($row = mysql_fetch_array($result))
{
    echo "<option value=".$row['Power'].">".$row['Power']." kW</option>";
}
// moshtnost avto
?>        
</select>


<script type="text/javascript">
  document.getElementById('Power').value = "<?php echo $_GET['Power'];?>";
</script>

<br />
Poles/Speed (RPM):<br>
<select name="Poles" id="Poles" onchange="this.form.submit()">
<option value="*">Show all available</option>
<option value="2">2 / 3000</option>
<option value="4">4 / 1500</option>
<option value="6">6 / 1000</option>
<option value="8">8 / 750</option>
</select>

<script type="text/javascript">
  document.getElementById('Poles').value = "<?php echo $_GET['Poles'];?>";
</script>
</form>

<?php
echo $_GET["Power"].",";
echo $_GET["Poles"];


if($_GET["Power"]!="*" and $_GET["Poles"]!="*") $query = "SELECT * FROM `TABLE 2` WHERE Power=".$_GET["Power"]." AND Poles=".$_GET["Poles"]."";
elseif($_GET["Poles"]!="*") $query.= "SELECT * FROM `TABLE 2` WHERE Poles=".$_GET["Poles"]."";
elseif($_GET["Power"]!="*") $query.= "SELECT * FROM `TABLE 2` WHERE Power=".$_GET["Power"]."";
else $query= "SELECT * FROM `TABLE 2` ";

$results = mysql_query($query)
or die(mysql_error());

echo '<table>';
echo '<tr><td><a href="?sort=id">Номер</a></td><td>ТИП</td><td>Мощност</td><td>Обороти</td><td>Тегло</td></tr>';
while ($row = mysql_fetch_array($results)) 
{
extract($row);

$arr=explode("-", $TYPE);

if ($arr[0]<='K2251S') $tip=$arr[0] ; elseif($arr[0]<='K2251S' && $Poles=2) $tip=$arr[0].'-2';

echo '<tr><td>'.$id.'</td><td><a href="motor.php?TYPE='.$tip.'">'.$TYPE.'</a></td><td>'.$Power.' kW</td><td>'.$Speed.'</td><td>'.$Weight.' кг.</td><td>'.$info.'</td></tr>';
}
echo '</table>';
?>
</body>

</html>

2 个答案:

答案 0 :(得分:0)

尝试添加几个栏位:

if ($arr[0]<='K2251S')
{
     $tip=$arr[0] ;  
}
elseif($arr[0]<='K2251S' && $Poles=2) 
{
        $tip=$arr[0].'-2';
}

答案 1 :(得分:0)

据我了解,您希望在$tip语句中替换“原样”echo,即更改类似

的内容
echo "xxx" . $tip . "xxx";

类似

echo "xxx" . if (condition1) $tip=$arr[0] ; elseif(condition2) $tip=$arr[0].'-2';

您应该在回显之前设置变量,即:尝试类似:

if (condition1) $tip=$arr[0] ; elseif(condition2) $tip=$arr[0].'-2';
echo "xxx" . $tip . "xxx";