0

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

どんな提案でも大歓迎です!

4

2 に答える 2

1

So you want an html structure, which always displays three products next to each other, and for each product places the image above the description which is above the link.

... and I wouldn't use a table for it.

I'm assuming you know how to fetch data from a database. You loop through the result table and the display as you need it:

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

This gives you a set of 3 elements next to each other, and within them 3 div elements above each other, each containing a detail about your product.

Here's the code for using a surrounding table:

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

... and here's as you requested in your question:

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>";
于 2012-11-08T16:20:27.347 に答える
0

セルを構築するためのループ。セルを数えながら、XXごとに(例では3番目)、行を閉じて新しいセルを開きます。

もっと必要かどうかはわかりませんが、それが基本です。

于 2012-11-08T14:58:19.763 に答える