2017-07-07 131 views
0

如何以表格形式顯示我的數據庫。在這裏我的代碼:如何使用PHP html以表格形式顯示數據庫?

<?php 

$link=mysqli_connect("localhost","root",""); 
mysqli_select_db($link,"order"); 
$res=mysqli_query($link,"select * from ordersum"); 
while($row=mysqli_fetch_array($res)) 
{ 

echo $row["name"]." ".$row["email"]." ".$row["content"]." ".$row["date"]." ".$row["amount"]; 
echo "<br>"; 

} 

?> 

enter image description here

回答

0

試試這個,希望能幫助你。

<table> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Email</th> 
       <th>Content</th> 
       <th>Date</th> 
       <th>Amount</th> 
      </tr> 
     </thead> 
    <tbody> 

<?php 
    $link=mysqli_connect("localhost","root",""); 
    mysqli_select_db($link,"order"); 
    $res=mysqli_query($link,"select * from ordersum"); 

    while($row=mysqli_fetch_array($res)){ 
     echo "<tr> 
       <td>".$row["name"]."</td> 
       <td>".$row["email"]."</td> 
       <td>".$row["content"]."</td> 
       <td>".$row["date"]."</td> 
       <td>".$row["amount"]."</td> 
       </tr>"; 
    } 
?> 
    </tbody> 
</table> 
+0

有對那裏發生的語法明智的東西時髦,但在其他方面,這是一個良好的開端。 – tadman

+0

haha​​hah,thankyouu先生:D –

+0

謝謝@Yudha Teguh Hartanto。願上帝保佑你。 :) –

0

您的代碼應該是這樣的:

<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Content</th> 
      <th>Date</th> 
      <th>Amount</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
<?php 
// your connection code here 
while($row=mysqli_fetch_array($res)) { ?> 
      <td><?php echo $row["name"];?></td> 
      <td><?php echo $row["email"];?></td> 
      <td><?php echo $row["content"];?></td> 
      <td><?php echo $row["date"];?></td> 
      <td><?php echo $row["amount"];?></td> 
<?php } ?> 
     </tr> 
    </tbody> 
</table> 

儘量不要回聲HTML標籤,這不是一個很好的做法,相反,近PHP,渲染HTML,呼應你的PHP值,並確保在它結束時關閉你的循環(while,for,foreach)。

+0

謝謝@Eduardo桑切斯。願上帝保佑你。 :) –

+0

@GeorgeLiewVuiChee不要忘記投我的答案: - D –

0

我嘗試使用此代碼。

<html> 
<body> 

<?php 
echo "<table style='border: solid 1px black;'>"; 
echo "<tr><th>Name</th><th>Email</th><th>Content</th><th>Date</th><th>Amount</th></tr>"; 

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 

    function current() { 
     return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>"; 
    } 

    function beginChildren() { 
     echo "<tr>"; 
    } 

    function endChildren() { 
     echo "</tr>" . "\n"; 
    } 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "order"; 

try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $conn->prepare("SELECT * FROM ordersum"); 
    $stmt->execute(); 

    // set the resulting array to associative 
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
     echo $v; 
    } 
} 
catch(PDOException $e) { 
    echo "Error: " . $e->getMessage(); 
} 
$conn = null; 
echo "</table>"; 
?> 

</body> 
</html> 

<?php 

$link=mysqli_connect("localhost","root",""); 
mysqli_select_db($link,"order"); 
$res=mysqli_query($link,"select * from ordersum"); 

?> 

enter image description here