php foreach偶数和奇数,但在打印2行后反之亦然
在我的应用程序中,我必须循环两种大小的图像。让我们把它们叫做大的和小的。。这些图像分为两列,如下所示php foreach偶数和奇数,但在打印2行后反之亦然,php,foreach,Php,Foreach,在我的应用程序中,我必须循环两种大小的图像。让我们把它们叫做大的和小的。。这些图像分为两列,如下所示 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");?>
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; ?>
我可以用什么方式来做这件事?我想我必须在每2个条目之后创建一种重置,并将奇数设置为eve,反之亦然?如果需要
AB;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
(基本上每第四行重复一次模式),那么一个清晰合理的方法是:
$row = 0;
foreach ($posts as $post) {
$temp = $row%4;
if ($temp == 0 || $temp == 3 ) echo "A\n"; else echo "B\n";
$row++;
}
这显然可以变得更加紧凑,但我通常发现“显式”在六个月后更容易阅读,而且对性能的影响可以忽略不计。尝试替换
<div class=" <?=(++$count%2 ? "col-7 pr-10" : "col-5 pl-10");?> mt-15">
这可能是css问题,而不是php问题。检查源代码的顺序是否正确。这不是css问题:)
<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">
<?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; ?>