drdawt9210 2014-05-08 16:47
浏览 57

php数组使用循环输出数据

I'd like to set the following up as a loop (so that the pattern is repeated every 5 elements). I've tried to read up on it and I believe I need to set it up as an array, but I have no idea where to start.

Here's my code:

<div class="ccm-page-list">

    <?php

        $i= 0;i;
        foreach ($pages as $page):
            $i++;

            $title  = $th->entities($page->getCollectionName());
            $url    = $nh->getLinkToCollection($page);
            $target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
            $target = empty($target) ? '_self' : $target;

            $description = $page->getCollectionDescription();
            $description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
            $description = $th->entities($description); 

            $img   = $page->getAttribute('thumbnail');
            $thumb = $ih->getThumbnail($img, 550, 550, true);

    ?>

    <a href="<?php  echo $url ?>" target="<?php  echo $target ?>"> <div class="col-sm-4  grid-item">
        <div class="item-img-blog" >
            <img src="<?php  echo $thumb->src ?>" width="auto" height="100%" alt="" />
            <div <?php 
                if($i == 1) {
                ?>class="item-class-1" <?php } 
                if($i == 3) {
                ?>class="item-class-2" <?php } 
                if($i == 2) {
                ?>class="item-class-3" <?php } 
                if($i == 4) {
                ?>class="item-class-3" <?php } 
                if($i == 5) {
                ?>class="item-class-1" <?php } 
            ?>>

                <div class="text-container">
                    <h1><?php  echo $description ?></h1>
                    <p><?php  echo $date = $page->getCollectionDatePublic('d/m/Y'); ?></p>
                </div>
            </div>
        </div>
    </div></a>
    <?php  endforeach; ?>
</div> 

I would really appreciate any help! Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dongsunny1113 2014-05-08 17:01
    关注

    You don't need an extra loop, you can use the modulo operator (aka, the remainder of a division) and some basic math to cope with the fact that you want to cycle 1->5:

    $i= 0;   // note that I removed the "i;" at the end of this line!
    foreach ($pages as $page):
       $cyclici = ($i % 5) + 1;
       // then, use $cyclici everywhere you want to see that 1-5 1-5 pattern
       ...
       if($cyclici == 1) {
       ...
    
       // and increment $i at the *end* of the loop instead of the start.
       $i++
    endforeach;
    

    EDITed to clarify the relationship between $i and $cyclici - $i is still incremented for each iteration of the loop, $cyclici is derived from that incrementing value to obtain the desired 1 2 3 4 5 1 2 3 4 5 ... sequence.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?