布局是块/模块化的,所以我需要为每个div应用一个类。问题是,当我在浏览器中预览时,在代码循环再次从案例1开始之后 - 它会跳过数据库中的一个项目,并将下一个项目显示为案例1.
$result = mysqli_query($connection, "SELECT * FROM products");
$index = 1;
while($row = mysqli_fetch_array($result)) {
$float = "";
switch ($index) {
case 1: $float = "large-left";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 2: $float = "mini-left";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 3: $float = "mini-right";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 4: $float = "mini-left";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 5: $float = "mini-right";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 6: $float = "large-right";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 7: $float = "mini-left";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 8: $float = "mini-right";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 9: $float = "mini-left";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
case 10: $float = "mini-right";
echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
break;
default: $index = 0;
break;
};
$index++;
};
mysqli_close($result);
?>
非常感谢任何建议/替代方法!
答案 0 :(得分:3)
你给$ index作为1
$index = 1;
因此,每次都会进入案例1。它不是动态的。
答案 1 :(得分:0)
如果我理解正确,你想要“分组”10种产品
$result = mysqli_query($connection, "SELECT * FROM products");
$index = 1;
while($row = mysqli_fetch_array($result)) {
if ($index % 10 == 1) {
$float = 'large-left';
}
else {
if($index % 2 == 1) {
$float = 'mini-right';
}
else {
$float = 'mini-left';
}
}
echo '<div class="' . $float . '"><img src="' . $row['product_imagepath'] . '"></div>';
$index++;
}