2013-08-24 74 views
1

在下面的代码中,当我执行它时,它不显示任何结果。但我没有在if-else条件下放置任何结果。实际结果:textbox,提交按钮,没有结果。我预期的结果是一个文本框,按钮应该显示,而不是没有results.With没有搜索数据它显示没有reults.So任何人帮助我。else语句显示

 <form action="<?php echo site_url('search1_site/index');?>" method = "post"> 
     <input type="text" name = "keyword" /> 
     <input type="submit" id="opn" value = "Search" /> 
     </form> 




        <?php 
      if($results){ 
      ?> <div id='hideme'> 
      CLOSE<a href='#' class='close_notification' title='Click to Close'> 
      <img src="<?php echo base_url('img/close.png'); ?>" width="15" height="15" alt="Close" onClick="hide('hideme')"/> 
      </a> <div style="background:#FFFFFF; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal" > 
      <table class="display2 table table-bordered table-striped"> 
      <tr> 

         <th>course_code</th> 
         <th>course name</th> 

        </tr> 
       <tr><?php 
      foreach ($results as $row) { 
       ?> 

      <td><?php echo $row->course_code;?></td> 
      <td><?php echo $row->course_name;?></td> 
      </tr> 
       <?php 
       } 
      }else{ 
     echo "no results"; 
      } 
      ?> 
     </table></div></div> 
     <script> 
     $('a.modal').bind('click', function(event) { 
    event.preventDefault(); 
     $('#modal').fadeIn(10); 
     }); 
    function hide(obj) { 
     var el = document.getElementById(obj); 
     el.style.display = 'none'; 
     } 
    </script> 

回答

0

您正在检查$results变量是否设置。

相反,您需要检查$results是否为空。

更换

if($results){ 

随着

if(!empty($results)){ 

,并确保$results包含数组。我的意思是你的变量必须有价值。

+0

没有搜索不显示任何结果@Dipesh帕尔马 – moses

+1

@moses确保您正在查询数据库以显示默认数据...在搜索记录之前.. –

0

据我可以告诉你的代码$结果没有设置。因此if ($results)评估为false,否则显示。

+0

未搜索到时不显示结果 – moses

0

核查请求是POST请求或默认页面加载

使用

else if($_SERVER['REQUEST_METHOD'] === 'POST') 
{ 
    echo "no results"; 
} 

,而不是

else 
{ 
    echo "no results"; 
}