PHP 回显表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15320429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:02:54  来源:igfitidea点击:

PHP echo tables

phphtmlmysqlcss

提问by Tomazi

Overview

概述

I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table. The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.

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

Here is what I have as a layout:

这是我的布局:

enter image description here

在此处输入图片说明

What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable

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

Something like this;

像这样的东西;

enter image description here

在此处输入图片说明

The code: PHP, MySQL & HTML:

代码: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 responsible for this part:

负责这部分的 CSS:

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


}

回答by Christophe

just add some padding or a border to the table cells:

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

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

}

Edit: What you could do as well:

编辑:您还可以做什么:

<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>

Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.

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

回答by Mr. Alien

First of all don't echo so much HTML using PHP, instead do it like this

首先不要使用 PHP 回显这么多 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"); ?> 

<table id='display'> 
    <?php 
        while($rows = mysql_fetch_array($sql)) { 
    ?>
       <tr><td><?php echo $rows[$product_name]; ?></td>
       <!-- And so on--> 
     <?php   
        } 
     ?> 
 </table>

Secondly by seeing your inmage, it seems like you need a border for your table so use

其次,通过查看您的图片,您的桌子似乎需要一个边框,因此请使用

table, td {
   border: 1px solid #000000;
}

回答by Scott Yang

add the following css to apply border on your table cells:

添加以下 css 以在表格单元格上应用边框:

#display td{ border: 2px solid red; }

and optionally add to your #display { :

并可选择添加到您的 #display { :

border-collapse: collapse;