只是想知道当我在搜索栏中键入一些内容时,如果没有任何内容与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
在while()循环之前将变量设置为0。在循环中设置为1。如果循环后仍为0,则打印一些文本。像这样:
$found=0;
while($row = mysql_fetch_array($result)){
$found=1;
...
}//end while loop
if ($found==0) {
echo "no results found";
}
您需要查看是否返回了任何行。根据php.net手册:
使用mysql_num_rows()查找SELECT语句或mysql_affected_rows()来查找行数受到DELETE、INSERT、REPLACE或UPDATE语句的影响。
像这样的东西会帮助你的代码:
...
$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%' ");
if (mysql_num_rows($result) == 0 ) {
// display no results found
} else {
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
}
}
...
}
简单。就拿那些CCD_ 1语句&将它们放在变量中。如果变量不为空,则echo
它。如果语句为空,那么echo
您的"未找到结果"消息。调整后的代码如下:
if(isset($_POST['submit'])){
$ret = '';
$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'];
$ret = "'n<div class='"thumb'">"
. "'n't<a href='"single.php?cid=$cid'"><img src='"thumbs/$file'" class='"thumbnailImg'" width='"150'" height='"200'"/></a><br/>"
. "'n't".$name. " "
. "'n't$". $price
. "'n</div>"
;
}//end while loop
}
// Check if '$ret' has content or not.
if (!empty($ret)) {
echo $ret;
}
else {
echo "<h2><em>No results were found.</em></h2>";
}
}//end if submit