Im trying to create a dynamic web page but have an issue with a for loop. In my html code I have a div called section group which can hold 4 columns at once. One product is shown in one column. In my database I have 13 products that means I want 13 columns to be dynamically generated. So to hold 13 columns 4 section group divs should also generated. The code is below.
<div class="menu_list">
<div class="section group">
<?php foreach($products as $product): ?>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<?php endforeach; ?>
</div>
</div>
Here is the php code.Im using PDO.
$products = $db->prepare("
SELECT *
FROM products
");
$products->execute();
$products = $products->fetchAll(PDO::FETCH_ASSOC);
What I want to happen is when 4 objects get pulled out of the database a new section group should be generated. So my final desire is this.
<div class="menu_list">
<div class="section group">
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
</div>
<div class="section group">
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
<div class="col_1_of_4 span_1_of_4">
<ul>
<li><a href="#"><span class="item_name"><?php echo $product['name']; ?></span></a></li>
</ul>
<img src="images/<?php echo $product['image']; ?>" alt="" />
<div class="button" align="center"><span><a href="buy.php">Buy Now</a></span></div>
</div>
</div>
</div>
I apologize if the code is too excessive. Ive tried so many for loops variations to no avail. Please help me if possible. Thank you all.
Aucun commentaire:
Enregistrer un commentaire