2017-04-10 225 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

相关问题