Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert <ul> in foreach loop every 10 records

<?php foreach($products as $product) : ?>

<li><a href="<?php echo base_url(); ?>main/products/<?php echo $product->id; ?>">
<?php echo $product->name; ?> </a></li>

<?php endforeach; ?>

So the code above get's all records in a DB and generates links. 100 Records in the table - I want to split the design into 5 col. So I will do that in CSS but I need to be able to enclose

<ul> on every 20 records</ul>

How can i count the loop and do this?

like image 858
MrPizzaFace Avatar asked Sep 28 '12 04:09

MrPizzaFace


1 Answers

Do like this , this will solve your problem

 <?php 
 *$i = 0;*
 echo "<ul>";
 foreach($products as $product) : 
 if($i % 20 == 0) echo "</ul><ul>"; 
?>
<li><a href="<?php echo base_url(); ?>main/products/<?php echo $product->id; ?>">
<?php echo $product->name; ?> </a></li>
<?php 
 //if($i % 20 == 0) echo "</ul>";
 $i += 1;
 endforeach; 
 echo "</ul>";
 ?>

*edit- set $i = 0; since arrays count from [0] by setting it to [1] makes the first col have 19 instead of 20. By setting it to [0] Each col has 20. :) Thanks again ~fabio

like image 135
Yogesh Suthar Avatar answered Nov 15 '22 14:11

Yogesh Suthar