else语句正在显示

时间:2013-08-24 06:00:05

标签: php codeigniter

在下面的代码中,当我执行它时它没有显示结果。但是我没有在if-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>

3 个答案:

答案 0 :(得分:0)

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

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

<强>替换

if($results){

。通过

if(!empty($results)){

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

答案 1 :(得分:0)

据我所知,你的代码中没有设置$ results。因此if ($results)评估为false,否则显示。

答案 2 :(得分:0)

检查请求是发布请求还是默认页面加载

使用

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

而不是

else
{
  echo "no results";
}