以下代碼將我的搜索結果回顯到頁面上。我試圖在沒有搜索結果時回顯消息,但似乎無法顯示消息。迴應消息,如果沒有搜索結果q
什麼是我檢查是否沒有結果的最佳方式?
代碼:
<?php if(isset($search_results)) {foreach($search_results as $result): ?>
<?php if(empty($search_results)) {echo 'No results';}
else {
echo '<div class="search_result"> <b>'.$result['title'].'</b><br />';
echo '<span class="search_result_url">'.$result['link'].'</span><br />';
echo $result['text'].'<br /></div>';
} ?>
<?php endforeach; } else {echo '<b>Please type what you are looking for into the search bar and press \'enter\'</b>';}?>
['空()'](http://php.net/manual/en前空/function.empty.php)是一個標準的PHP函數。 – Farray
是的,但海報沒有說明結果是「空的」。它也可以包含像「
」這樣的html標籤。在這些情況下,empty()將返回FALSE。但我想我應該澄清,更在我的答案.. – alex