php - 按条目字段将 mysql 获取的结果显示到按行组织的表中?

标签 php html mysql

我有一个名为 product 的 mysql 表,其结构如下:

CREATE TABLE product (
    id int(6) unsigned NOT NULL auto_increment,
    name varchar(100) NOT NULL default '',
    image varchar(255) NOT NULL default '',
    link varchar(255) NOT NULL default '',
    price decimal(3,2) NOT NULL default '0.00',
    PRIMARY KEY (id)
);

我想将检索到的结果{product1,...,product5}可视化为 3 列的 html/php 表,其结构如下:

<table border="1" cellpadding="2" cellspacing="2" width="100%">
    <tr>
       <td>product1.image</td>   <td>product2.image</td>   <td>product3.image</td>
    </tr>
    <tr>
       <td>product1.name</td>    <td>product2.name</td>    <td>product3.name</td>
    </tr>
    <tr>
       <td>product1.link</td>    <td>product2.link</td>    <td>product3.link</td>
    </tr>

    <tr> 
         <td colspan="3">&nbsp;</td>
    </tr>

    <tr>
       <td>product4.image</td>   <td>product5.image</td>  <td>&nbsp;</td>
    </tr>
    <tr>
        <td>product4.name</td>   <td>product5.name</td>  <td>&nbsp;</td>
    </tr>
    <tr>
        <td>product4.link</td>   <td>product5.link</td>  <td>&nbsp;</td>
    </tr>
</table>

请注意,给定条目的字段(例如 product1.{image,description,link})显示在不同的行中。 下面的代码说明了如何显示某些产品的图像。

$table = 'product';
$result = mysql_query("SELECT id, image, description, link FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td> <img src="$field->image" border="0"></td>";

    if($i%3==0){echo"</tr><tr>";}
}
echo "</tr></table>";

如有任何建议,我们将不胜感激!

最佳答案

因此,您需要一个 html 结构,它始终相邻显示三个产品,并且对于每个产品,将图像放置在链接上方的描述上方。

...我不会使用表格。

我假设您知道如何fetch data from a database 。 您可以根据需要循环遍历结果表和显示内容:

$counter = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<span><div>".$row["image"]."</div><div>".$row["description"]."</div><div>".$row["link"]."</div></span>;
    $counter += 1;
    if($counter % 3 = 0)
    {
        echo "</br>
    }
}

这将为您提供一组 3 个彼此相邻的元素,其中有 3 个彼此上方的 div 元素,每个元素都包含有关您产品的详细信息。

这是使用周围表格的代码:

echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'><tr>";
$counter = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<td><div>".$row["image"]."</div><div>".$row["description"]."</div><div>".$row["link"]."</div></td>;
    $counter += 1;
    if($counter % 3 = 0)
    {
        $echo "</tr><tr>";
    }
}
echo "</tr></table>";

...这是您在问题中所要求的:

echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'>";
$counter = 1;
$htmlHolder1 = "<tr>";
$htmlHolder2 = "<tr>";
$htmlHolder3 = "<tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $htmlHolder1 .= "<td>".$row["image"]."</td>";
    $htmlHolder2 .= "<td>".$row["description"]."</td>";
    $htmlHolder3 .= "<td>".$row["link"]."</td>";
    $counter += 1;
    if($counter % 3 = 0)
    {
        echo $htmlHolder1."</tr>";
        echo $htmlHolder2."</tr>";
        echo $htmlHolder3."</tr>";

        $htmlHolder1 = "<tr>";
        $htmlHolder2 = "<tr>";
        $htmlHolder3 = "<tr>";
    }
}
if($counter % 3 != 0)
{
    echo $htmlHolder1."</tr>";
    echo $htmlHolder2."</tr>";
    echo $htmlHolder3."</tr>";
}
echo "</table>";

关于php - 按条目字段将 mysql 获取的结果显示到按行组织的表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291147/

相关文章:

php - 插件中的 wp_schedule_event() 未安排 cron 事件

php - PHP 如何处理将自身作为元素引用的数组?

php - Lumen 数据库播种错误

javascript - 如何使用 Chrome.Storage?

html - 如何同时拥有水平和垂直导航栏

mysql - 使用嵌套选择更新 MySql 中的重复值

php - 在php中匹配并访问带有数据库列的字符串

php - 无法在 Linux (Ubuntu 12.04 LTS) 上安装 MongoDB PHP 驱动程序

javascript - 如何在$.ajax方法的success属性中访问对象的元素?

javascript - 获取弹出窗体中按钮的值