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 に答える