如何在每10条记录中在foreach循环中插入

时间:2022-10-06 19:30:34
<?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

所以上面的代码获取数据库中的所有记录并生成链接。表中的100条记录 - 我想将设计拆分为5列。所以我会在CSS中这样做,但我需要能够附上

<ul> on every 20 records</ul>

How can i count the loop and do this?

我如何计算循环并执行此操作?

5 个解决方案

#1


8  

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

* edit- set $ i = 0;因为数组从[0]设置为[1]使得第一个col有19而不是20.通过将其设置为[0]每个col都有20. :)再次感谢~fabio

#2


2  

This should work

这应该工作

<?php $products = array('1','2','3','4','5','6','8', 9, 10, 11, 12);?>
<?php $count = 0?>
<?php foreach($products as $product) : ?>
<?php $count = $count + 1; ?>
<?= (($count == 5) ? "<ul>" : '' )?>
<li><a href="<?php echo base_url(); ?>main/products/"></a></li>

<?= (($count == 5) ? "</ul>" : '')?>
<?php (($count == 5) ? $count = 0 : '' )?>
<?php endforeach; ?>

#3


2  

 <?php 
     $i = 1;
     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 
     $i += 1;
      endforeach; echo "</ul>"; ?>

#4


0  

Checkout php's array_slice method. It will allow you to slice the array into smaller arrays of 20 objects each.

Checkout php的array_slice方法。它允许您将数组切割成每个包含20个对象的较小数组。

#5


0  

<?php    $i=0;
         echo "<ul>";
         foreach($products as $product) :
         if($i%8==0&&$i!=0) echo "</ul><ul>"; ?>

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

<?php    $i+=1;
         endforeach;
         echo "</ul>"; ?>

#1


8  

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

* edit- set $ i = 0;因为数组从[0]设置为[1]使得第一个col有19而不是20.通过将其设置为[0]每个col都有20. :)再次感谢~fabio

#2


2  

This should work

这应该工作

<?php $products = array('1','2','3','4','5','6','8', 9, 10, 11, 12);?>
<?php $count = 0?>
<?php foreach($products as $product) : ?>
<?php $count = $count + 1; ?>
<?= (($count == 5) ? "<ul>" : '' )?>
<li><a href="<?php echo base_url(); ?>main/products/"></a></li>

<?= (($count == 5) ? "</ul>" : '')?>
<?php (($count == 5) ? $count = 0 : '' )?>
<?php endforeach; ?>

#3


2  

 <?php 
     $i = 1;
     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 
     $i += 1;
      endforeach; echo "</ul>"; ?>

#4


0  

Checkout php's array_slice method. It will allow you to slice the array into smaller arrays of 20 objects each.

Checkout php的array_slice方法。它允许您将数组切割成每个包含20个对象的较小数组。

#5


0  

<?php    $i=0;
         echo "<ul>";
         foreach($products as $product) :
         if($i%8==0&&$i!=0) echo "</ul><ul>"; ?>

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

<?php    $i+=1;
         endforeach;
         echo "</ul>"; ?>