只是想知道当我在搜索栏中输入内容但没有任何内容与 MySQL 数据库中存储的内容匹配时,如何显示此消息。
到目前为止我所拥有的是这个。
<?php
if(isset($_POST['submit'])){
$search = trim($_POST['search']);
if($search != ""){
//echo "search: ". $search;
$result = mysql_query("SELECT * FROM catalogue WHERE
name LIKE '$name' OR
category LIKE '$category' OR
brand LIKE '$brand' OR
season LIKE '$season' OR
price LIKE '$price' OR
store LIKE '$store' OR
description LIKE '%$search%' ");
while($row = mysql_fetch_array($result)){
$name = $row['name'];
$file = $row['file'];
$description = $row['description'];
$category = $row['category'];
$brand = $row['brand'];
$season = $row['season'];
$price = $row['price'];
$store = $row['store'];
$cid = $row['cid'];
echo "\n<div class=\"thumb\">";
echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
echo "\n\t".$name. " ";
echo "\n\t$". $price;
echo "\n</div>";
}//end while loop
}else{
echo "<h2><em>No results were found.</em></h2>";
}//end if search ,else
}//end if submit
?>
如果我只是单击搜索而不输入任何内容,则此代码片段有效,但如果我在搜索中输入不匹配的内容,则不会显示任何内容。我该如何解决这个问题?
最佳答案
设置一个标志计数器,你就会让它工作。
$results=0; // Setting a flag here
while($row = mysql_fetch_array($result)){
$name = $row['name'];
$file = $row['file'];
$description = $row['description'];
$category = $row['category'];
$brand = $row['brand'];
$season = $row['season'];
$price = $row['price'];
$store = $row['store'];
$cid = $row['cid'];
echo "\n<div class=\"thumb\">";
echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
echo "\n\t".$name. " ";
echo "\n\t$". $price;
echo "\n</div>";
$results++; //Incrementing flag if results found.
}//end while loop
}
else if($results==0)
{
echo "<h2><em>No results were found.</em></h2>";
}
else{
echo "<h2><em>No results were found.</em></h2>";
}//end if search ,else
关于php - 如何显示 "No Results Found"消息 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450958/