2015-06-07 158 views
0
<?php 
    include "db.php"; 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $search_date = $_POST['search']; 
    } 

    $show_result = mysqli_query($con, "SELECT * FROM data_input where input_date = '$search_date' "); 
    // var_dump($show_result); 
    $row_count = mysqli_num_rows($show_result); 
    echo $row_count; 

    if ($show_result){ 
     ?> 

     <table class="table table-bordered table-striped"> 

      <tr> 
       <th>id</th> 
       <th>date</th> 
       <th>items</th> 
       <th>description</th> 
       <th>cost</th> 
       <th>person</th> 
      </tr>  
     <?php 
    } 
    else{ 

     if ($row_count == 0) {?> 

     <tr> 
      <td colspan="6"> 
       <?php echo "no data found"; ?> 
      </td> 
     </tr> 
      <?php 
     } 

    } 
    while($row = mysqli_fetch_object($show_result)){?> 
     <tr> 
      <td> 
       <?php echo $row->id ?> 
      </td> 
      <td><?php echo $row->input_date ?></td> 
      <td><?php echo $row->input_items ?></td> 
      <td><?php echo $row->input_description ?></td> 
      <td><?php echo "$".$row->input_cost ?></td> 
      <td><?php echo $row->input_person ?></td> 
     </tr> 

    <?php 


    }?> 
    </table> 

    <a href="index1.php" class="btn btn-default">Back</a> 
    <?php 

?> 

這是一個搜索結果頁面。一個包含搜索框和提交按鈕的簡單表單。首先輸入日期和查詢發現的數據,然後在表中顯示數據,但是如果沒有數據在表中沒有發現任何數據。我該如何解決這個問題?謝謝你的回答。如何在沒有數據的情況下製作表格行?

回答

2

您在if ($show_result)else分支中有if ($row_count == 0)
這是不正確的,因爲mysqli_query只有在出現錯誤時才返回false。如果結果中沒有行,則不行。

解決方案:在創建表頭之後將if ($row_count == 0)塊向上移動到右側。

if ($show_result){ 
     ?> 

     <table class="table table-bordered table-striped"> 

      <tr> 
       <th>id</th> 
       <th>date</th> 
       <th>items</th> 
       <th>description</th> 
       <th>cost</th> 
       <th>person</th> 
      </tr>  
     <?php 
     if ($row_count == 0) {?> 

     <tr> 
      <td colspan="6"> 
       <?php echo "no data found"; ?> 
      </td> 
     </tr> 
      <?php 
     } 

    } 
    else{ 
    } 

(然後else塊是空的,所以你可以在這種情況下刪除它,或者你可以迴應什麼地方出了錯一些診斷。)

0

你應該循環的行前創建表,後行

  • 的那張支票號碼,如果使用一會兒,然後顯示您的錶行else loop它。 以下我已經完成了。

    ID 日期 項目 描述 成本 人

    <?php 
        if ($row_count == 0) { 
          ?> 
    
          <tr> 
           <td colspan="6"> 
            <?php echo "no data found"; ?> 
           </td> 
          </tr> 
         <?php 
         } else { 
         while ($row = mysqli_fetch_object($show_result)) { 
          ?> 
          <tr> 
           <td> 
            <?php echo $row->id ?> 
           </td> 
           <td><?php echo $row->input_date ?></td> 
           <td><?php echo $row->input_items ?></td> 
           <td><?php echo $row->input_description ?></td> 
           <td><?php echo "$" . $row->input_cost ?></td> 
           <td><?php echo $row->input_person ?></td> 
          </tr> 
    
         <?php 
         } 
        }?> 
    </table> 
    <a href="index1.php" class="btn btn-default">Back</a> 
    
0

請在下面的代碼替換while循環代碼。如果找不到數據,則任何行都會顯示字符串'not found'。另外我用mysqli_fetch_assoc代替mysqli_fetch_object。

while($row = mysqli_fetch_assoc($show_result)){ ?> 
    <?php if(!empty($row)){ ?> 
      <tr> 
       <td> 
        <?php array_key_exists('id', $row) && $row['id'] != "" ? $id = $row['id']: "not found"; echo $id; ?> 
       </td> 
       <td><?php array_key_exists('input_date', $row) && $row['input_date'] != "" ? $input_date = $row['input_date']: "not found"; echo $input_date; ?></td> 
       <td><?php array_key_exists('input_items', $row) && $row['input_items'] != "" ? $input_items = $row['input_items']: "not found"; echo $input_items; ?></td> 
       <td><?php array_key_exists('input_description', $row) && $row['input_description'] != "" ? $input_description = $row['input_description']: "not found"; echo $input_description; ?></td> 
       <td><?php array_key_exists('input_cost', $row) && $row['input_cost'] != "" ? $input_cost = $row['input_cost']: "not found"; echo "$".$input_cost; ?></td> 
       <td><?php array_key_exists('input_person', $row) && $row['input_person'] != "" ? $input_person = $row['input_person']: "not found"; echo $input_person; ?></td> 
      </tr> 
    <?php }else if(empty($row)){ ?> 
      <tr> 
       <td> 
        <?php echo "not found"; ?> 
       </td> 
       <td><?php echo "not found"; ?></td> 
       <td><?php echo "not found"; ?></td> 
       <td><?php echo "not found"; ?></td> 
       <td><?php echo "not found"; ?></td> 
       <td><?php echo "not found"; ?></td> 
      </tr>   
<?php } }?> 
相關問題