简体   繁体   English

如何将 HTML 表格变成网格?

[英]How to make HTML table into a grid?

I'm retrieving data from my database into a table, but the data is piling up to the left rather than a grid view.我正在将数据库中的数据检索到表中,但数据堆积在左侧而不是网格视图中。

    include_once ('includes/db_connect.php');
    $res = mysql_query("SELECT * FROM inventory ORDER BY id DESC");

    echo "<table>";
    while ($row = mysql_fetch_array($res)) {
        echo "<tr>"; echo "<td>";?> <img src="<?php echo $row["image"]; ?>" height="200" width="280">  <?php echo "</tr>";
        echo "<tr>"; echo "<td>"; echo $row['make']; echo "&nbsp; "; echo $row['model']; echo "</td>";  echo "</tr>";
    }
    echo "</table>";

Any help will be highly appreciated.任何帮助将不胜感激。

You just need closing /td tags for every opening td tag.您只需要为每个打开的 td 标签关闭 /td 标签。 Your first line is missing a /td:您的第一行缺少 /td:

echo "<tr>"; echo "<td>";?> <img src="<?php echo $row["image"]; ?>" height="200" width="280">  <?php echo "</td></tr>";

echo "<tr>"; echo "<td>"; echo $row['make']; echo "&nbsp; "; echo $row['model']; echo "</td></tr>";

You probably also want to style your table with this inside the head tags:您可能还想在 head 标签中使用此样式来设置表格的样式:

<style>
table {
    border:1px solid black;
    border-collapse:collapse;
}
</style>

Or inline styles:或内联样式:

<table style="border:1px solid black; border-collapse:collapse;">

In your code, the end of the first line closes off the table row without actually closing off the cell.在您的代码中,第一行的结尾关闭了表格行,而实际上并未关闭单元格。 I have also spaced out your code as to improve readability.我还将您的代码隔开以提高可读性。 I believe the issue was the missing </td> at the end of the first row.我认为问题在于第一行末尾缺少</td>

$res = mysql_query("SELECT * FROM inventory ORDER BY id DESC");

echo "<table>";

while ($row = mysql_fetch_array($res)) {
    ?>
    <tr><td>
    <img src="<?php echo $row["image"]; ?>" height="200" width="280">
    </td></tr>
    <tr><td>
    <?php echo $row['make']; echo "&nbsp; "; echo $row['model'];?>
    </td></tr>
    <?php
}
echo "</table>";

It also should be noted that your mysql_query should be switched over to mysqli or pdo.还应该注意的是,您的 mysql_query 应该切换到 mysqli 或 pdo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM