在 Html 表中显示 PHP 数组结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13961388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Php Array result in an Html table
提问by Obed Lorisson
I was trying to display an array in php to an HTML table but there's an issue. I found several examples here in stack overflow, but they don't work for my case.
我试图将 php 中的数组显示到 HTML 表中,但存在问题。我在堆栈溢出中找到了几个示例,但它们不适用于我的情况。
Controller:
控制器:
<?php include('inc/db_connect.php');?>
<?php
try
{
$sql = "SELECT id GroupName, VideoTITLE, ByArtist FROM videoclip";
$result = $pdo->query($sql);
}
catch(PDOException $e)
{
$error = 'unable to fetch data: '.$e->getMessage();
include'error.html.php';
exit();
}
$URLS = array();
while ($row = $result->fetch())
{
$URLS[] = array('id' => $row['id'], 'GroupName' => $row['GroupName'], 'VideoTITLE' => $row['VideoTITLE'], 'ByArtist'=> $row['ByArtist'] );
}
html:
html:
<div id="table_admin" class="span7">
<h3>Videoclip List</h3>
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>Song name</th>
<th>Group name </th>
<th>Artist </th>
</tr>
</thead>
<?php foreach ($URLS as $URL){
echo'<tbody>';
echo'<tr>';
echo'<td>'. $row['VideoTITLE']."</td>";
echo'<td>'. $row['GroupName'].'</td>';
echo'<td>'. $row['ByArtist'].'</td>';
echo'<tr>';
echo'</tbody>';
}
?>
</table>
</div>
回答by John Conde
You're close:
你很接近:
</thead>
<tbody>
<?php
foreach ($URLS as $URL){
echo'<tr>';
echo'<td>'. $URL['VideoTITLE']."</td>";
echo'<td>'. $URL['GroupName'].'</td>';
echo'<td>'. $URL['ByArtist'].'</td>';
echo'<tr>';
}
?>
</tbody>
Since you're taking the values of the $URLSarray and calling each one $URLyou need to refer to $URLfor each row's value. Not the $rowvariable you originally used to populate the array from the database results.
由于您正在获取$URLS数组的值并调用每个值,因此$URL您需要$URL为每一行的值引用。不是$row您最初用于从数据库结果填充数组的变量。
FYI, you may want to look into htmlentities()to escape your data to help prevent XSS attacks.
仅供参考,您可能需要考虑htmlentities()转义数据以帮助防止 XSS 攻击。
回答by Clay McIlrath
Assuming your data from the fetchRow is good to go, I see only one major bug in your html: change $row to $URL in your foreach loop. Also, you can mix PHP with HTML to make it a little more pretty:
假设您从 fetchRow 获取的数据很好,我在您的 html 中只看到一个主要错误:在您的 foreach 循环中将 $row 更改为 $URL。此外,您可以将 PHP 与 HTML 混合使用,使其更漂亮一点:
<?php foreach ($URLS as $URL) { ?>
<tr>
<td><?php echo $URL['VideoTITLE']; ?></td>
<td><?php echo $URL['GroupName']; ?></td>
<td><?php echo $URL['ByArtist']; ?></td>
<tr>
<?php } ?>
回答by Hardik Masalawala
After getting the result from database please follow
从数据库中获取结果后,请遵循
<?php
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th>Song name</th>';
echo '<th>Group name </th>';
echo '<th>Artist </th>';
echo '</tr>';
echo '</thead>'
echo '<tbody>';
foreach($URLS as $URL) {
echo'<tr>';
echo'<td>'. $URL['VideoTITLE']."</td>";
echo'<td>'. $URL['GroupName'].'</td>';
echo'<td>'. $URL['ByArtist'].'</td>';
echo'<tr>';
}
echo '</tbody>';
echo '</table>';
?>
I hope this help you
我希望这对你有帮助
回答by veltriv
yea as john said, you have $URLs as $URL, but then your referring to an array calling $row in the for each.
是的,正如约翰所说,你有 $URLs 作为 $URL,但是你引用了一个数组,在每个数组中调用 $row。
the other thing is that you may want to take the tbody tags outside of the for each loop
另一件事是您可能希望将 tbody 标签放在 for each 循环之外

