<?php
$output = "";
if(isset($_POST["query"]))
{
$search = $_POST["query"];
$sql = ("
SELECT * FROM `products`
WHERE productName LIKE :sText
OR productLine LIKE :sText
OR productVendor LIKE :sText
OR MSRP LIKE :sText
");
$stmt=$db->prepare($sql);
}
elseif
{
$sql = "SELECT * FROM products";
$stmt=$db->prepare($sql);
}
$stmt->execute(array(':sText'=>'%'. $search .'%'));
foreach($stmt as $row)
{
$output = "
<div class='product_wrapper'>
<form method='post' action=''>
<table style='width:170px;height:143px;''>
<tr>
<td><input type='hidden' name='productCode' value=". $row['productCode']." />
<div class='name'>".$row['productName']."</div>
<div class='price'>RM". $row['MSRP']."</div></td>
</tr>
<table style='width:170px;height:60px'>
<tr>
<td><button type='submit' class='buy' style='margin: : 25px;'>Buy Now</button></td>
</tr>
</table>
</table>
</form>
</div>
";
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
我对elseif条件所需的条件有什么疑问,以便添加最后未找到的else echo数据。我必须找出可以添加的elseif条件。感谢您的帮助。
答案 0 :(得分:0)
您不能将else与foreach一起使用,因此需要在使用foreach语句之前检查是否有任何结果。
if($stmt->rowCount() > 0) {
foreach($stmt as $row)
{
$output = echo"
<div class='product_wrapper'>
<form method='post' action=''>
<table style='width:170px;height:143px;''>
<tr>
<td><input type='hidden' name='productCode' value=". $row['productCode']." />
<div class='name'>".$row['productName']."</div>
<div class='price'>RM". $row['MSRP']."</div></td>
</tr>
<table style='width:170px;height:60px'>
<tr>
<td><button type='submit' class='buy' style='margin: : 25px;'>Buy Now</button></td>
</tr>
</table>
</table>
</form>
</div>";
$output;
}
} else {
echo 'Data Not Found';
}
答案 1 :(得分:0)
您可以使用rowCount
来获取受影响的行数
尝试一下
if($stmt->rowCount() > 0) {
//Loop throught the rows
}else{
echo "No matching records found.";
}
请检查此以获取更多详细信息PDOStatement::rowCount