gpt4 book ai didi

php - 按顺序在 5 个项目后添加 div

转载 作者:行者123 更新时间:2023-12-03 03:02:43 27 4
gpt4 key购买 nike

我需要在每 5 个项目后添加一个 div 标签。任何人都可以为这一逻辑提供任何帮助吗?我已经尝试过,但没有成功。

我有这个代码:

<div class="product-grid">
<?php foreach ($products as $product) { ?>
<div class="item">.....</div>
<?php } ?>
</div>

我明白了

<div class="product-grid">
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
</div>

看起来像这样:(按顺序在 5 个项目后添加一个 div。)

<div class="product-grid">
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="clearfix visible-xs-block"></div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="clearfix visible-xs-block"></div>
<div class="item">.....</div>
</div>

一些帮助将不胜感激。

最佳答案

<?php 
// control variable
$counter = 0;
?>
<div class="product-grid">
<?php foreach ($products as $product) { ?>
<div class="item">.....</div>
<?php
// on every #5 result
if(++$counter % 5 === 0) { ?>
<div class="clearfix visible-xs-block"></div>
<?php
}
}
?>
</div>

关于php - 按顺序在 5 个项目后添加 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com