您应该使用PHPmodulo operator 为此。由于您需要两种不同的布局,您应该根据$count % 2
和($count + 1) % 2
:
<?php
$count = 0;
foreach ($howtoplaycontent as $hpc): ?>
<div class="row courses_row" style="background-color: #f8f8f8">
<div class="container">
<div class="row" style="padding: 15px;">
<div class="col-lg-6 course_col order-lg-<?php echo $count % 2 + 1; ?>">
</div>
<div class="col-lg-6 course_col order-lg-<?php echo ($count + 1) % 2 + 1; ?>">
</div>
</div>
</div>
</div>
<?php $count ++;
endforeach; ?>
Update
由于偶数行和奇数行需要不同的背景色,我建议您在CSS中定义两个额外的类:
.row.courses_row.odd_row {
background-color: #f8f8f8;
}
.row.courses_row.even_row {
background-color: #ffffff;
}
然后使用以下代码:
<?php
$count = 0;
foreach ($howtoplaycontent as $hpc): ?>
<div class="row courses_row <?php echo $count % 2 ? \'even\' : \'odd\'; ?>_row">
<div class="container">
<div class="row" style="padding: 15px;">
<div class="col-lg-6 course_col order-lg-<?php echo $count % 2 + 1; ?>">
</div>
<div class="col-lg-6 course_col order-lg-<?php echo ($count + 1) % 2 + 1; ?>">
</div>
</div>
</div>
</div>
<?php $count ++;
endforeach; ?>