2017-01-16 13 views
1

我有一個五行的表,我寫了一個php代碼來打印表中的所有行。 但不幸的是我在想念somecode這實際上觸發獲得,而不是從表中的一行數據的連續性..如何使用PHP打印表中的所有行而不是一個?

我的代碼如下:

<?php 
$sql2 = "SELECT * FROM useraddmoneyhistory ORDER BY date DESC"; 
$result2 = $conn->query($sql2); 
if ($result2->num_rows > 0) { 
    $index = 0; 
    while($row2 = $result2->fetch_assoc()) { 
     $index++; 
     ?>      
     <table class="table table-hover" > 
      <tr> 
       <th>ID</th> 
       <th>Mobile</th> 
       <th>Earlier Balance</th> 
       <th>Amount</th> 
       <th>Discount</th> 
       <th>Date</th> 
      </tr> 

      <tr > 
       <td><?php echo ($row2["id"]); ?></td> 
       <td><?php echo ($row2["mobile"]); ?></td> 
       <td><?php echo ($row2["preamount"]); ?></td> 
       <td><?php echo ($row2["amount"]); ?></td> 
       <td><?php echo ($row2["amountdis"]); ?></td> 
       <td><?php echo ($row2["date"]); ?></td> 
      </tr> 
     </table> 

任何幫助是極大的讚賞...

+1

把你的HTML放到你的循環中。否則,你只能得到最後一行。 –

+0

你的右大括號在哪裏? –

+0

我有他們,但我沒有在問題中提到他們.. – harishk

回答

1

你必須把html放入循環中,在你的代碼whileif聲明中沒有關閉。

<table class="table table-hover" > 
<tr> 
<th>ID</th> 
<th>Mobile</th> 
<th>Earlier Balance</th> 
<th>Amount</th> 
<th>Discount</th> 
<th>Date</th> 
</tr> 

<?php 
if ($result = $mysqli->query($query)) { 

/* fetch associative array */ 
while ($row = $result->fetch_assoc()) { 
    ?> 
    ... HTML 'tr' CODE ... 
<?php 

} // close while 
    $result->free(); 
} //close if 
print "</tr></table>"; 
?> 
相關問題