【问题标题】:How do you display a "No Results Found" message (PHP)如何显示“未找到结果”消息 (PHP)
【发布时间】:2013-12-25 09:38:14
【问题描述】:

只是想知道当我在搜索栏中输入内容时如何显示此消息,但没有任何内容与存储在我的 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
?>

如果我只单击搜索而不输入任何内容,则此代码 sn-p 有效,但如果我在搜索中输入不匹配的内容,则不会显示任何内容。我该如何解决?

【问题讨论】:

  • if ( mysql_num_rows($result) == 0 ){ echo "No results found!"; die(); }

标签: php mysql database search


【解决方案1】:

设置一个标志计数器,你会得到它的工作。

$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

【讨论】:

  • 谢谢!我现在有类似的东西,它可以工作:)
【解决方案2】:

在 while() 循环之前将变量设置为 0。在循环内将其设置为 1。如果循环后仍然为 0,则打印一些文本。像这样:

$found=0;
while($row = mysql_fetch_array($result)){
    $found=1;
    ...
}//end while loop
if ($found==0) {
    echo "no results found";
}

【讨论】:

    【解决方案3】:

    您需要查看是否返回了任何行。根据php.net manual:

    使用 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
    
        }
      }
    ...
    
    
    }
    

    【讨论】:

      【解决方案4】:

      简单。只需将这些 echo 语句放在变量中即可。如果变量不为空,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
      

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 1970-01-01
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 2014-09-21
        • 2020-05-14
        • 2021-05-11
        相关资源
        最近更新 更多