我会尝试解释我的问题,但我的英语不好,所以我希望你能理解我。
目前,当我在管理页面中添加产品时,它们会在一行中显示在另一个旁边,例如,如果我添加10个产品,我必须向右滚动页面才能看到它们。
现在我想在5个产品的网格中显示它们。因此,当我添加产品时,其中五个显示在一行中,然后第六个产品显示在第一个产品下面,依此类推。我真的非常需要这个,所以每一个帮助都会很有用,谢谢你的每一个答案。
这是代码
<?php
include "storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 10");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
echo"<table width='100%' border='0' cellspacing='5' cellpadding='6'>";
echo "<tr>";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$count = 0;
if($count == 5) {
$count = 0;
echo "</tr> <tr>";
}
echo "<td width='17%' valign='top'><a href='product.php?id=" . $id . "'><img `enter code here`style='border:#666 1px solid;' src='inventory_images/" . $id . ".jpg' `enter code here`alt='" . $product_name . "' width='77' height='102' border='1' /> </a>`enter code here`</td>";
echo "<td width='83%' valign='top'>" . $product_name . "<br />
$" . $price . "<br />";
echo "<a href='product.php?id=" . $id . "'>View Product Details</a></td>";
}
}else {
echo "We have no products listed in our store yet";
}
echo"</tr>";
echo"</table>";
mysql_close();
?>
答案 0 :(得分:0)
声明$count=1;
一边while
循环
像这样使用
if($count % 5==0) {
echo "</tr> <tr>";
}
并且在while循环结束之前增加$count++
答案 1 :(得分:0)
if ($productCount > 0) {
echo"<table width='100%' border='0' cellspacing='5' cellpadding='6'>";
$count = 0;
while($row = mysql_fetch_array($sql)){
echo "<tr>";
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
echo "<td width='17%' valign='top'><a href='product.php?id=" . $id . "'><img style='border:#666 1px solid;' src='inventory_images/" . $id . ".jpg' `enter code here`alt='" . $product_name . "' width='77' height='102' border='1' /> </a>`enter code here`</td>";
echo "<td width='83%' valign='top'>" . $product_name . "<br />
$" . $price . "<br />";
echo "<a href='product.php?id=" . $id . "'>View Product Details</a></td>";
if($count%5==0) {
echo "</tr> <tr>";
}
echo "</tr> ";
}
$count++;
}