donglunzai4288 2011-10-31 12:00
浏览 13
已采纳

如果没有搜索结果,则回显消息

The following code echos my search results on to the page. I am trying to echo a message when there are no search results but can't seem to get the message to show.

What is the best way for me to check if there are no results??

Code:

    <?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>';}?>
  • 写回答

2条回答 默认 最新

  • duanche4578 2011-10-31 13:53
    关注

    You need to check whether your $search_results array is empty before the foreach loop

     <?php 
    if( isset($search_results) ) {
        if( empty($search_results) ) {
            echo 'No results';
        } else {
            foreach($search_results as $result) {
                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>';
             }
        } 
    } else {
        echo '<b>Please type what you are looking for into the search bar and press \'enter\'</b>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题