PDO MySQL 产品循环
PDO MySQL product loop
在查询和 PDO 方面,我是一个真正的初学者,我在忙于的项目中需要一些帮助。我想要完成的是显示数据库中的产品。然而,我使用的模板样式迫使我每行显示 3 个产品,有没有办法每行显示 3 个产品并在超过 3 个时循环代码(如果有 5 个产品,前 3 个将显示在第一行,其余在第二行)。
这是我正在使用的模板,请注意 div class "top-box",这会强制每行只能显示 3 个产品。
<div class="top-box">
<?php
$sql = "SELECT * FROM _products WHERE category = '$cat'";
$result = dbConnect()->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$cols = $result->columnCount(); // Number of returned columns
// Generate ADS Feed for each ROW
foreach($result as $row) {
echo '<div class="col_1_of_3 span_1_of_3">
<a href="product.php?i=' . $row['model'] . '">
<div class="inner_content clearfix">
<div class="product_image">
<img src="images/' . $row['model'] . '.jpg" alt=""/>
</div>
<div class="price">
<div class="cart-left">
<p class="title">' . $row['name'] . '</p>
</div>
<div class="clear"></div>
</div>
</div>
</a>
</div>';
}
} else {
echo "<p>No products available at this moment, contact us for more information!</p>";
}
?>
<div class="clear"></div>
</div>
添加一个计数器并使用%
算术运算符计算列号。
$counter=0;
foreach (...) {
$column_number=$counter % 3;
$counter++;
}
您可以在产品的每个循环中使用计数变量。每三个产品你可以关闭顶盒并打开一个新的(这是一个假设,因为我不知道你的风格是如何工作的)。
foreach 循环结束时:
if ($count != 0 && $count % 3 == 0) {
echo '<div class="clear"></div>';
echo '</div>'; // close last .top-box
echo '<div class="top-box">';
}
++$count;
我看不出您的问题与 PDO 有何关联。请记住,在查询中使用未转义的变量有潜在的危险。看看这里寻求帮助:
你可以这样解决:
<?php
if ($counter % 3 == 0 && $counter!=$total_row_fetched) {
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="top-box">';
}
if($counter==$total_row_fetched){
echo '<div class="clear"></div>';
echo '</div>'; // it will close the last open div
}
++$counter;
?>
在查询和 PDO 方面,我是一个真正的初学者,我在忙于的项目中需要一些帮助。我想要完成的是显示数据库中的产品。然而,我使用的模板样式迫使我每行显示 3 个产品,有没有办法每行显示 3 个产品并在超过 3 个时循环代码(如果有 5 个产品,前 3 个将显示在第一行,其余在第二行)。
这是我正在使用的模板,请注意 div class "top-box",这会强制每行只能显示 3 个产品。
<div class="top-box">
<?php
$sql = "SELECT * FROM _products WHERE category = '$cat'";
$result = dbConnect()->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$cols = $result->columnCount(); // Number of returned columns
// Generate ADS Feed for each ROW
foreach($result as $row) {
echo '<div class="col_1_of_3 span_1_of_3">
<a href="product.php?i=' . $row['model'] . '">
<div class="inner_content clearfix">
<div class="product_image">
<img src="images/' . $row['model'] . '.jpg" alt=""/>
</div>
<div class="price">
<div class="cart-left">
<p class="title">' . $row['name'] . '</p>
</div>
<div class="clear"></div>
</div>
</div>
</a>
</div>';
}
} else {
echo "<p>No products available at this moment, contact us for more information!</p>";
}
?>
<div class="clear"></div>
</div>
添加一个计数器并使用%
算术运算符计算列号。
$counter=0;
foreach (...) {
$column_number=$counter % 3;
$counter++;
}
您可以在产品的每个循环中使用计数变量。每三个产品你可以关闭顶盒并打开一个新的(这是一个假设,因为我不知道你的风格是如何工作的)。
foreach 循环结束时:
if ($count != 0 && $count % 3 == 0) {
echo '<div class="clear"></div>';
echo '</div>'; // close last .top-box
echo '<div class="top-box">';
}
++$count;
我看不出您的问题与 PDO 有何关联。请记住,在查询中使用未转义的变量有潜在的危险。看看这里寻求帮助:
你可以这样解决:
<?php
if ($counter % 3 == 0 && $counter!=$total_row_fetched) {
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="top-box">';
}
if($counter==$total_row_fetched){
echo '<div class="clear"></div>';
echo '</div>'; // it will close the last open div
}
++$counter;
?>