PHP以网格模式动态显示图像

时间:2016-11-22 13:44:39

标签: php

我是php的新手,我正在使用一个关于在页面上动态显示图像的教程,它工作正常,但它会垂直显示它们,我希望它们是水平的。我创建了一个包含代码的页面,但我似乎无法确定在何处插入代码以显示图像。 谢谢你的帮助。 垂直输出看起来像这样

$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added ASC LIMIT 6");
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $details = $row["details"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="1000px" border="0" cellspacing="0" cellpadding="6" align="center">
        <tr>
          <td width="1000px" align="center"><a href="product.php?id=' . $id . '"><img style="border:#666 0px solid;" src="images/' . $id . '.jpg" width="50%" height="50%" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br /> $' . $details . '<br />
            <a href="product.php?id=' . $id . '">order</a></td>
        </tr>
      </table>';
}
mysql_close();
?>

Grid Output

$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15"); 
$i = 0;
// Establish the output variable
$dynamiclist = '<table width="1000px" border="1" cellspacing="2" cellpadding="10" align="center">';
while($row = mysql_fetch_array($sql)){ 

    $id = $row["id"];
    $product_name = $row["product_name"];
    $details = $row["details"];
    $price = $row["price"];

    if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
        $dynamiclist .= '<tr><td>' . $product_name . '</br>' . $details . '</br>' . $price . '</td>';

    } else {

        $dynamiclist .= '<td>' . $product_name . '</td>';
    }
    $i++;
}
$dynamiclist .= '</tr></table>';
?>

1 个答案:

答案 0 :(得分:0)

我明白了,谢谢你的帮助。

$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15"); 
$i = 0;
// Establish the output variable
$dynamiclist = "";
while($row = mysql_fetch_array($sql)){ 

    $id = $row["id"];
    $product_name = $row["product_name"];
    $details = $row["details"];
    $price = $row["price"];


    if ($i % 4 == 0) { // if $i is divisible by our target number (in this case "3")
        $dynamiclist .= '<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
            ' . $details . '<br /> $' . $price . '<br />
            <a href="product.php?id=' . $id . '">order</a></td>';

    } else {

        $dynamiclist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
            ' . $details . '<br /> $' . $price . '<br />
            <a href="product.php?id=' . $id . '">order</a></td>';
    }
    $i++;
}
$dynamiclist .= '</tr></table>';
?>