簡體   English   中英

PHP MySQL在HTML中獲取數組結果

[英]Php mysql fetch array result in html

我正在使用以下代碼來顯示mysql數據。 問題是在HTML中顯示數據時,數據顯示在一行中,並且沒有被包裝。

<?php 
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
while($res = mysqli_fetch_array($result)) { 
    echo "<tr>";
    echo "<td>".$res['topic']."</td>";
    echo "<td>".$res['issue']."</td>";
    echo "<td>".$res['solution']."</td>";
    echo "<td><div align='center'><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";       
}
?>

請幫助我的代碼。

您缺少關閉<tr>標記的信息,因此,所有數據都在同一行中。

while($res = mysqli_fetch_array($result)) { 
    echo "<tr>";
    echo "<td>".$res['topic']."</td>";
    echo "<td>".$res['issue']."</td>";
    echo "<td>".$res['solution']."</td>";
    echo "<td><div align='center'><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";  
    echo "</tr>"; // << close <tr> tag     
}

在要換行的地方使用<br>

首先,您必須打印<table>以創建表

echo "<table>";
.
.
.
.
echo "</table>";

 without table tag<br> <tr> <td>Peter</td> <td>Griffin</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> </tr> <br> with table taaag <table> <tr> <td>Peter</td> <td>Griffin</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> </tr> </table> 

嘗試此演示,對於演示,請使用數組而不是表數據,

<?php
$data=array(array("id"=>1,"topic"=>"php","issue"=>"var 
error","solution"=>"declare 
first"),array("id"=>2,"topic"=>"asp","issue"=>"var error 
validatipn","solution"=>"validate js add")); 
?>
<table border="1">
    <thead>
    <tr><th>topic</th><th>issue</th><th>solution</th><th>Action</th></tr>
    </thead>
    <tbody>
<?php
 foreach($data as $res)
 { 
 echo "<tr>";
 echo "<td>".$res['topic']."</td>";
 echo "<td>".$res['issue']."</td>";
 echo "<td>".$res['solution']."</td>";
 echo "<td><div align='center'><a href=\"edit.php?id=$res[id]\">Edit</a> | 
 <a 
href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you 
want to delete?')\">Delete</a></td></tr>";       
 }
 ?>
 </tbody>
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM