i am just learning about PHP for simple e-commerce site and i have to fetch data from database (8 products) and i want to make them only shown 4 products each line then it must printed on new line. how to do it using PHP?
<?php
include 'source/db.connect.php';
$result = $source->query("SELECT * FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.productCategory = CATEGORIES.categoryId");
while ($row = $result->fetch(PDO::fetch_assoc)) {
echo "<div class=\\"products-box\\">";
echo "<p>Product Image: {$row['productImage']}."</p>";
echo "<p>Product Name: {$row['productName']}."</p>";
echo "<p>Product Price: {$row['productPrice']}."</p>";
echo "</div>";
}
That’s my code but it’s not auto move to new line. sorry for my bad english
The simplest way to do this is to wrap four of the product-box divs in their own outer div. The outer div will define a row of products. You could do something along the following:
$cnt = 0;
echo "<div class=\\"product-row\\">"; // start the first row
while ($row = $result->fetch(PDO::fetch_assoc)) {
if ($cnt > 0 && ($cnt % 4) == 0)
{
echo "</div>"; // close the current row
echo <div class=\\"product-row\\">"; // start a new row
}
++$cnt; // increment count
echo "<div class=\\"products-box\\">";
echo "<p>Product Image: {$row['productImage']}."</p>";
echo "<p>Product Name: {$row['productName']}."</p>";
echo "<p>Product Price: {$row['productPrice']}."</p>";
echo "</div>";
}
echo "</div>"; // close the last row
With the query, you’re using the SELECT * but only displaying three fields, if there is actually more then three fields in the table, you should specify them in the SELECT clause instead or using the SELECT *
[SIZE=2][FONT=trebuchet ms]hi, i am just use above code and it’s work but i look something wrong. You can look at bellow image:
why i have look any additional padding / margin in there but I never add it. But, everything just find when i add product over simple html & css markup without any query to database just printed as normal html. really confused for me.
[/FONT][/SIZE]