我是一個真正的初學者,當涉及到查詢和PDO時,我需要一些我正忙於的項目的幫助。我想要完成的是從數據庫中顯示產品。然而,我使用的模板風格迫使我每行顯示3個產品,是否有一種方法可以顯示每行3個產品,並在代碼超過3個時循環代碼(如果有5個產品,前3個會顯示在第一行,其餘的在第二行)。PDO MySQL產品循環
這裏是我使用的模板,請注意div類「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>
使用PDO這樣並不比使用'mysql_query'更好。這同樣很危險 –