2017-04-10 222 views
-1

我試圖從數據庫將數據輸出到我的網頁上。我能夠如此成功,但卻非常不整潔。我試圖把數據放在網站上的表格中,但沒有成功。使用php/html將數據輸出到HTML表格中

下面,數據是從數據庫中檢索,但只是粗略地迴應。它看起來凌亂,但它成功地輸出到網頁

<?php 
     $query = "SELECT name, email, address FROM SHHowners"; 
     $result = mysqli_query($conn, $query); 

     if (mysqli_num_rows($result) > 0) { 
      // output data of each row 
      while ($row = mysqli_fetch_assoc($result)) { 
       echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>"; 
      } 
     } else { 
      echo "0 results"; 
     } 


     mysqli_close($conn); 

我嘗試把數據轉換成HTML表格(在同一頁上,通過PHP和HTML沒有成功結合。如何把這些數據轉化爲有組織的?表成功

<div class="container"> 
    <h2>Bordered Table</h2> 
    <p>The .table-bordered class adds borders to a table:</p>    
    <table class="table table-bordered"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Address</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td><?php $row["name"] ?></td> 
     <td><?php $row["email"] ?></td> 
     <td><?php $row["address"]?></td> 
     </tr> 
    </tbody> 
    </table> 
</div> 

下面大致是怎麼想的數據進行結構化,如何才能做到這一點

Name | Email | Address 
Jo |[email protected]|12 Street 
Ben |[email protected]|23 street 
+1

您輸出表格的嘗試擺脫了*所有的PHP *。循環在哪裏? echo聲明在哪裏?您可以採用工作代碼中的確切結構,並將HTML結構放在需要的地方。 – David

回答

1

試試這個:

<div class="container"> 
    <h2>Bordered Table</h2> 
    <p>The .table-bordered class adds borders to a table:</p>    
    <table class="table table-bordered"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Address</th> 
     </tr> 
    </thead> 
    <tbody> 
<?php 
while ($row = mysqli_fetch_assoc($result)) { 
?> 
     <tr> 
     <td><?php $row["name"] ?></td> 
     <td><?php $row["email"] ?></td> 
     <td><?php $row["address"]?></td> 
     </tr> 
<?php } ?> 
    </tbody> 
    </table> 
</div> 
0
<?php 
     $query = "SELECT name, email, address FROM SHHowners"; 
     $result = mysqli_query($conn, $query); 
?> 
<div class="container"> 
    <h2>Bordered Table</h2> 
    <p>The .table-bordered class adds borders to a table:</p>    
    <table class="table table-bordered"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Address</th> 
     </tr> 
    </thead> <tbody> 
<?php 
     if (mysqli_num_rows($result) > 0) { 
      // output data of each row 
      while ($row = mysqli_fetch_assoc($result)) { 
?> 
     <tr> 
     <td><?php $row["name"] ?></td> 
     <td><?php $row["email"] ?></td> 
     <td><?php $row["address"]?></td> 
     </tr> 
<?php   } 
     } else { 
      echo "<tr><td colspan=3>0 results</td></tr>"; 
     } 
     mysqli_close($conn); 
?> 
    </tbody> 
    </table> 
</div> 
1

試試下面的代碼:

<div class="container"> 
    <h2>Bordered Table</h2> 
    <p>The .table-bordered class adds borders to a table:</p> 
    <table class="table table-bordered"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Address</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
    <?php 
     $query = "SELECT name, email, address FROM SHHowners"; 
     $result = mysqli_query($conn, $query); 
     if (mysqli_num_rows($result) > 0) { 
     // output data of each row 
     while ($row = mysqli_fetch_assoc($result)) { ?> 
      <td><?php $row["name"]?></td> 
      <td><?php $row["email"]?></td> 
      <td><?php $row["address"]?></td> 
     <?php } 
      } ?> 
     </tr> 
    </tbody> 
    </table> 
</div> 
0

你總是可以從選擇打印結果是這樣的:

while ($row = mysqli_fetch_assoc($result)) { 
    echo "<tr> 
<td>".$row["name"]."</td> 

<td>".$row["email"]."</td> 

<td>".$row["address"] . "</td></tr>"; 

} 

這不是很乾淨,但對於每一個行應該打印另一行,低於三個表格標題。

0

除了其他答案之外,還沒有一種非常乾淨的方法可以做到這一點。

但是你可以做的是儘可能地將HTMLPHP分開放在同一個文件中。

PHP在文件的頂部可以如下:

$query = "SELECT name, email, address FROM SHHowners"; 
$result = mysqli_query($conn, $query); 

$htmlToDisplay = ''; //this variable will contain table rows 

if (mysqli_num_rows($result) > 0) { 
    // output data of each row 
    while ($row = mysqli_fetch_assoc($result)) { 
    //create the HTML row - we'll use this later 
    $htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>"; 
    } 
} else { 
    $htmlToDisplay = "<tr><td>0 results</td><tr>"; 
} 

然後你就可以在你的收盤PHP標籤(?>)後,有你HTML如下:

<table class="table table-bordered"> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Address</th> 
    </tr> 
    </thead> 
    <tbody> 
    <!-- echo your php row(s) here in a slightly neater way since it's one line --> 
    <?php echo $htmlToDisplay; ?> 
    </tbody> 
</table> 

這將使文件的不同部分(PHPHTML)更具可讀性。

你也可以看看使用PHP模板引擎如smarty

相關問題