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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示