0

在下面的代码中,我正在搜索一个值。如果搜索到该值,它将显示结果,但如果该值不存在,则不应显示任何结果。就我而言,如果我执行而不搜索任何值,它不会显示任何记录。请任何人帮助我。

    <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>
4

2 回答 2

0

尝试将name属性添加到您的提交按钮并检查它是否在if...else块之前设置。

<!-- HTML -->
<input type="submit" name="submit" id="opn" value = "Search"  />

// PHP

<?php
if (isset($_POST['submit'])) { // Here
  // Do the search here

        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";
        }
} // If closing
?> 

这与您正在寻找的东西相似吗?

于 2013-08-24T06:54:26.350 回答
0

如果变量$results是结果数组,请使用以下内容:

if (count($results) > 0)
{
 // show results
}   else
{
echo "No results";
}
于 2013-08-24T06:49:58.627 回答