PHP 回显表

标签 php html mysql css

概览

我有一些与产品相关的数据存储在 MySql 数据库中。我正在尝试检索此数据并使用 HTML 表格将其显示在页面上。 PHP 和 MySql 运行良好,所有数据都已检索,但显示方式非常困惑。

这是我的布局:

enter image description here

我的目标是进一步划分结果表添加更多列行以使数据更具可读性

类似这样的东西;

enter image description here

代码:PHP、MySQL 和 HTML:

<?php

    session_start();

    include('connect_mysql.php');


    $product_name = 'product_name';
    $product_qua = 'product_qua';
    $product_price = 'product_price';
    $product_image = 'product_image';
    $product_des = 'product_des';


$sql = mysql_query("SELECT * FROM products");

 echo "<table id='display'>";


while($rows = mysql_fetch_array($sql))
{
    echo"<br>";
    echo"<tr><td>";
    echo"$rows[$product_name]<br></td>";

    echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
    echo"<td>Avalible: $rows[$product_qua]<br></td>";
    echo"<td>Price: $rows[$product_price]<br></td>";
    echo"<td>Description: $rows[$product_des]<br></td>";    
    echo"</tr>";

}

echo "</table>";
?>

负责这部分的CSS:

#display{
    float:left;
    border: 5px solid black;
    margin-left:100px;


}

最佳答案

只需在表格单元格中添加一些填充或边框:

table#display td{
    border: 1px solid black;
    padding:0 8px;

}

编辑:您还可以做什么:

<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
    <!-- <br>  <- why a break? it's not in the correct spot anyway --> 
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>    
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>

提示:我更喜欢使用 while/endwhile;向用户显示数据时,而不是使用括号的方法。

关于PHP 回显表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320429/

相关文章:

mysql - SQL 中的方程 (MySQL)

php - PDO 和 MySQLi 准备好的语句有什么区别?

php - Kartik select2 小部件在 Yii2 的 Ajax 请求中不起作用?

php - 如何通过 cpanel/whm API 创建数据库?

javascript - 2 个与响应式下拉(汉堡)菜单相关的问题

html - 电子邮件表格宽度对于描述来说太小了

html - CSS, 2 div, 1 expanding 1 fixed, 但需要换行

php - PHP 中的静态实例

mysql - 每小时平均 time()

数字后的 MYSQL REGEXP_REPLACE