doumanni3501 2013-02-12 16:09
浏览 30
已采纳

在php中为嵌套循环提供逻辑帮助

Let's say I have 8 php objects and I need to iterated through them and output various information into html differently according to their placement. I have developed the structure for the css and html and you can see it here: http://jsfiddle.net/v3qcc/9/ I just need help with the logic of placing them their dynamically.

I need to take every two objects and place them in this wrapper:

<div class="col">
    <div class="third">
        <div class="col-content">         
          **...Object One Here**
        </div>
    </div>

    <div class="fourth">
        <div class="col-content">         
          **...Object Two Here**
        </div>
    </div>
</div>

<div class="col">
    <div class="third">
        <div class="col-content">         
          **...Object Three Here**
        </div>
    </div>

    <div class="fourth">
        <div class="col-content">         
          **...Object Four Here**
        </div>
    </div>
</div>
  • 写回答

3条回答 默认 最新

  • duanmo6937 2013-02-12 16:42
    关注
    // heres a class
    class thing {
    public $ybob = 23;
    }
    
    // create an array of 8 of them
    $i = 0;
    $a= array();
    while(8> $i){
    $a[$i] = new thing;
    $i++;
    }
    //var_dump($a);
    
    // loop thru the array of objects
    $rows = 1;
    foreach($a as $ob){
    
    // use modulus operator to work out if a row is odd or even
    echo ( ($rows % 2)  === 1 ) ?  '<br />NEW ROW' : '' ;
    echo $ob->ybob . ' - ' ;  // output the objects data in any case
    $rows++;
    }
    

    In the ternary operator 4 rows up, you can add the extra row tag, or revert an if/else if you wanted to use many more lines of markup.

    Gives:

    NEW ROW 23 - 23 - 
    NEW ROW 23 - 23 - 
    NEW ROW 23 - 23 - 
    NEW ROW 23 - 23 -
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看