我试图将 php 中的数组显示到 HTML 表中,但出现了问题。我在堆栈溢出中找到了几个示例,但它们不适用于我的情况。
控制器:
<?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:
<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>