【问题标题】:php array output data using a loop使用循环的php数组输出数据
【发布时间】:2014-06-26 04:53:17
【问题描述】:

我想将后续设置为一个循环(这样模式每 5 个元素重复一次)。我试图阅读它,我相信我需要将它设置为一个数组,但我不知道从哪里开始。

这是我的代码:

<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 作为1 2 3 4 5 6 7 8 9 你实际上想要1 2 3 4 5 1 2 3 4 5 等等,这是否正确?
  • 是的,这正是我想做的!对不起,我有点过头了!

标签: php arrays loops iteration


【解决方案1】:

您不需要额外的循环,您可以使用modulo operator(也就是除法的余数)和一些基本数学来处理您想要循环 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;

经过编辑以阐明 $i 和 $cyclici 之间的关系 - $i 在循环的每次迭代中仍然递增,$cyclici 是从该递增值派生的,以获得所需的1 2 3 4 5 1 2 3 4 5 ... 序列。

【讨论】:

  • 做到了!非常感谢! (无需澄清):)
  • 我认为澄清仍然可以帮助未来的访问者,所以我将保留它。很高兴我能帮你解决你的问题:)
【解决方案2】:
$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)];    
  .....
}

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2021-12-08
    相关资源
    最近更新 更多