首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php数组使用循环输出数据

php数组使用循环输出数据
EN

Stack Overflow用户
提问于 2014-05-09 00:47:02
回答 2查看 106关注 0票数 0

我想把下面的代码设置为一个循环(这样每隔5个元素就会重复一次)。我试着阅读它,我认为我需要将它设置为一个数组,但我不知道从哪里开始。

下面是我的代码:

代码语言:javascript
复制
<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> 

如果有任何帮助,我将不胜感激!提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2014-05-09 01:01:07

你不需要额外的循环,你可以使用modulo operator (也就是除法的剩余部分)和一些基本的数学运算来处理你想要循环1->5的事实:

代码语言:javascript
复制
$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为了阐明$i和$cyclici之间的关系- $i对于循环的每次迭代都是递增的,$cyclici是从该递增值导出的,以获得所需的1 2 3 4 5 1 2 3 4 5 ...序列。

票数 1
EN

Stack Overflow用户

发布于 2014-05-09 01:10:29

代码语言:javascript
复制
$i= 0;
$classes = array(1 => 1, 2 => 3, 3 => 2, 4 => 3, 5 => 1);
foreach ($pages as $page) {
  $i++;
  .....
  echo 'class="item-class-'.$classes[$i % (count($classes) + 1)];    
  .....
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23547841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档