2013-12-08 33 views
0

想知道如何在我的搜索欄中輸入內容時顯示此消息,並且沒有任何內容與我的MySQL數據庫中存儲的內容相匹配。如何顯示「未找到結果」消息(PHP)

到目前爲止我所擁有的就是這個。

<?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 
?> 

這段代碼工作,如果我只需點擊搜索,而無需輸入任何東西,但如果我在不匹配搜索輸入的東西,什麼也不顯示。我如何解決這個問題?

+1

'if(mysql_num_rows($ result)== 0){echo「No results found!」;死(); }' – Hussain

回答

0

在while()循環之前將變量設置爲0。在循環內將其設置爲1。如果循環後仍然爲0,則打印一些文本。像這樣:

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

謝謝!這很簡單,完美:) – aihao

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 
+0

謝謝!我現在有類似的東西,它的工作原理:) – aihao

0

您需要查看是否有任何行被返回。按照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 

    } 
    } 
... 


} 
0

容易。只要把那些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 
相關問題