PHP-代码为数据库中的每个记录创建单独的表

时间:2018-09-28 06:44:32

标签: php html mysqli

我正在尝试创建一个表,该表将遍历数据库中的所有记录,但似乎是为每个记录创建单独的表。

$query = 'select * from images';
$result = mysqli_query($connection,$query);

$row_count = mysqli_num_rows($result);


for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';


foreach($row as $next) {
{
  echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";

echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
    echo '</section>';
}

1 个答案:

答案 0 :(得分:1)

理想情况下,您会绕着tr

echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";


foreach($row as $next) {
{
    echo "<tr>";
    echo "<td>" . $next['imageid'] . "</td>";
    echo "<td>" . $next['imagename'] . "</td>";
    echo "<td>" . $next['description'] . "</td>";
    // all the other columns 
    echo "</tr>";
}

echo "</table>";