在指定次数的循环迭代后插入一个块


Inserting a block after a specified number of loop iterations

我试图在ZF1环境下从数据库获取一些信息。我在这里用了一个for循环。我想要完成的是为每四个迭代插入一个新的css划分(div)。换句话说,我想插入一个块,后跟四个循环迭代。例如,如果我运行一个循环12次,那么在每4次迭代之后应该有3个新的块。它基本上是一个广告块,可以包含图像和链接。我需要一个主意。

这是一个示例块。

<div class="flexblock-image">
    <a href="https://offers.example.com/offer/600112?cid=BWO_mh.com_html_getbackinshape&amp;keycode=254194">
        <img alt="" title="" typeof="foaf:Image" src="http://www.example.com.gif">
        </a>
    </div>

这是我在我的应用程序中编写的循环。

<?php if ($this->numArticles > 0) : ?>
<?php for ($i = 0; $i < 13; $i++) : ?>
<div class="channel-image ">
    <div class="img">
        <h2 class="primary-tag">
            <a href="/tags/weight-loss-tips" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
                <?php echo $this->articles[$i]->slug ; ?>
            </a>
        </h2>
        <a href="
            <?php echo $this->articles[$i]->getViewUrl(); ?>">
            <img typeof="foaf:Image" src="
                <?php echo $this->articles[$i]->getCover('crop'); ?>" width="650" height="412" alt="
                <?php echo addslashes($article->title); ?>" title="
                <?php echo addslashes($article->title); ?>">
            </a>
        </div>
        <div class="channel-content">
            <p class="date">
                <span property="dc:date dc:created" content="2015-06-04T11:06:18-04:00" datatype="xsd:dateTime">
                    <?php echo $this->dateFormatter()->diff(strtotime($this->articles[0]->activated_date), $this->timeDiffFormats); ?>
                </span>
            </p>
            <h2 class="article-title" id="title-node-93726">
                <a href="
                    <?php echo $this->articles[$i]->getViewUrl(); ?>">
                    <?php echo addslashes($this->articles[$i]->title); ?>
                </a>
            </h2>
            <div class="byline-container">
                <span class="byline-role">By  </span>
                <span class="field-author">
                    <a href="#" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
                        <?php echo sprintf($this->translator()->_('show.by'), $this->articles[0]->getAuthor()); ?>
                    </a>
                </span>
            </div>
        </div>
    </div>
    <?php endfor; ?>
    <?php endif; ?>

不是PHP专家,但for循环内部的if语句不会通过使用模数运算符工作吗?

<?php for ($i = 0; $i < 13; $i++) : ?>
    <?php if ($i % 4 === 0) : ?>
        // Custom HTML/CSS here
    <?php endif; ?>
<?php endfor; ?>

关于模数运算符的更多信息请参见