2016-09-20 79 views
1

我已經嘗試過了,但是我無法知道我在哪裏做錯了。該表只連續回顯第一行,並沒有回顯其他行。請幫助。使用php創建動態數據庫表db

$run = " 
       SELECT * FROM staff 
       "; 
     $runquery = mysqli_query($connection, $run); 
     $runrow = mysqli_num_rows($runquery); 
     if($runrow < 1){ 
      echo "<p class='errormsg'>You do not have any Staff</p>"; 
     } 
     else{ 
      $row = mysqli_fetch_array($runquery); 
      if($row) { 
       $surname = $row['surname']; 
       $lastname = $row['lastname']; 
       $phone = $row['phone']; 
       $username = $row['username']; 
       $role = $row['auth']; 
      } 
      foreach ($row as $staff) { 
       $table .= " 
         <tr> 
          <td>$surname</td> 
          <td>$phone</td> 
          <td>$username</td> 
          <td>$role</td> 
         </tr> 
      "; 
      } 

     } 
+0

因爲您在循環之前設置變量。 – Noman

+0

不工作。我試過 –

+0

檢查我的答案 – Noman

回答

0

你需要像這樣改變循環。你的記錄的表格視圖也需要<table>正文。

$run  = "SELECT * FROM staff"; 
$runquery = mysqli_query($connection, $run); 
$runrow = mysqli_num_rows($runquery); 
if($runrow < 1) { 
    echo "<p class='errormsg'>You do not have any Staff</p>"; 
} 
else { 
    $table = ""; // create table here 
    while($row = mysqli_fetch_array($runquery)) { 
     $surname = $row['surname']; 
     $lastname = $row['lastname']; 
     $phone = $row['phone']; 
     $username = $row['username']; 
     $role  = $row['auth']; 
     $table .= "<tr> 
          <td>$surname</td> 
          <td>$phone</td> 
          <td>$username</td> 
          <td>$role</td> 
         </tr>"; 
    } 
    echo $table; 
} 
+0

嗯...支持讚賞。但只有最後一行才顯示,僅此而已。代碼需要改進。我真的很感謝你的幫助。請幫助 –

+0

它正常工作正常,嘗試'print_r($ row);'in循環 – Noman

0

您是否嘗試將以下代碼部分移至foreach循環中?

$run = " 
     SELECT * FROM staff 
     "; 
$runquery = mysqli_query($connection, $run); 
$runrow = mysqli_num_rows($runquery); 
if($runrow < 1){ 
    echo "<p class='errormsg'>You do not have any Staff</p>"; 
} 
else{ 
    $row = mysqli_fetch_array($runquery); 
    foreach ($row as $staff) { 
     $surname = $staff['surname']; 
     $lastname = $staff['lastname']; 
     $phone = $staff['phone']; 
     $username = $staff['username']; 
     $role = $staff['auth']; 
     $table .= " 
       <tr> 
        <td>$surname</td> 
        <td>$phone</td> 
        <td>$username</td> 
        <td>$role</td> 
       </tr> 
    "; 
    } 

}