在我的应用程序中,我必须循环两个大小的图像。让我们把它们叫做大大小小。这些图像分为两列,如下所示。
large small
large small
large small我现在这样做是通过这样做来产生一个类的幼虫/小图像:
<?php $count = 0; ?>
<?php foreach ($posts as $post) : ?>
<div class=" <?=(++$count%2 ? "col-7 pr-10" : "col-5 pl-10");?> mt-15">
<?php endforeach; ?>我现在想要做的是编制这份清单;
large small
small large
large small
etc..我能用什么样的方式做这件事?我想我必须创建一种重置后,每两个条目,并设置奇数除夕,反之亦然?
发布于 2014-04-03 15:42:55
通常,如果在连续循环中需要A B; B A; A B;等,可以这样做:
$row = 0;
foreach ($posts as $post) {
if ($row%2) echo "A, B\n"; else echo "B, A\n";
$row++;
}这看起来很像您的代码…
如果你真的是说你需要
A
B
B
A(从本质上讲,每4行重复一次模式),那么一种清晰合理的方法是:
$row = 0;
foreach ($posts as $post) {
$temp = $row%4;
if ($temp == 0 || $temp == 3 ) echo "A\n"; else echo "B\n";
$row++;
}这显然可以变得更紧凑--但我通常发现“显式”在六个月后更容易阅读--而且性能影响可以忽略不计。
发布于 2014-04-03 15:43:36
试着替换
<div class=" <?=(++$count%2 ? "col-7 pr-10" : "col-5 pl-10");?> mt-15">使用
<div class=" <?=($count%4 == 1 || ? $count%4 == 2)"col-7 pr-10" : "col-5 pl-10"; $count++;?> mt-15">发布于 2014-04-03 15:46:41
<?php $count = 0; $flag = 0;?>
<?php foreach ($posts as $post) : ?>
<?php $flag = $count%2 == 0 ? $flag + 1 : $flag;?>
<div class=" <?=(($count++ +$flag)%2 ? "col-7 pr-10" : "col-5 pl-10");?> mt-15">
<?php endforeach; ?>https://stackoverflow.com/questions/22842416
复制相似问题