繁体   English   中英

PHP的条件

[英]Conditions with PHP

我对PHP还是很陌生,试图找出我在工作中的网站上发现的问题。 在屏幕快照中,最后一个div.image-group应该在div.image-row里面,里面只有两个项目-基本上,我一直希望div.image-group放在div.image-row里面,应该最多容纳三个div.image-group (但第一个div.image-row可能包含更少的)。

在此处输入图片说明

谁能指出正确的方向来纠正这个问题?

这是代码:

<?php $counter = 0; ?>
    <?php foreach ($rows as $id => $row): ?>
        <?php $counter++; ?>
        <?php if ($counter % 3 == 1 || $counter === 1) { ?>
            <div class="image-row small clearfix">
        <?php } ?>
                <div class="image-group"><?php print $row; ?></div>
        <?php if (($counter != 1 && $counter % 2 == 1) || ($id == count($rows) -1 && $counter % 2 != 1)) { ?>   
            </div>
        <?php } ?>
    <?php endforeach; ?>

如果在开始时将原始数组分为三个一组,则代码将更简单。

<?php foreach (array_chunk($rows, 3) as $image_row): ?>
    <div class="image-row small clearfix">
        <?php foreach ($image_row as $image_group): ?>
            <div class="image-group"><?= $image_group ?></div>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

第二个条件应该是:

    <?php if ($counter % 3 == 0 || $counter == count($rows)) { ?>

$counter % 3 == 0在每第三行之后为true,而$counter == count($rows)在最后一行之后为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM