duanduo7400 2011-04-15 13:57
浏览 27
已采纳

php foreach循环帮助

I have a foreach loop that builds me a product page basically it out puts my products in rows of 3.

See the code:

foreach ($product_sets as $product)
{
    $currentRow = ceil($currentItem / 3);
    $currentColumn = $currentItem - (($currentRow - 1) * 3);
    if ($number_of_blanks == 2) :
        if (($number_of_rows > 1 && $currentRow == ($number_of_rows - 1) && $currentColumn == 2) || ($number_of_rows == 1 && $currentColumn == 1)) :
    ?>
            <li><img src="<?php echo site_url('assets/img/blocks/guarantee.png'); ?>" alt="5 Year Guarantee" width="242" height="156"></li>
    <?php
            $currentItem++;
        endif;
    endif;
    ?>
    <li class="<?php if($currentItem % 3 == 0) echo 'endHomeBlock';?>">
        <?php $this->load->view('blocks/product_small', array('product' => $product)); ?>
    </li>
    <?php
        $currentItem++;

    }

What I am wanting be able to do is place an image (A point of sale) at the end of the first row, and the randomly through the other rows, but maintaining 3 items (including image a point of sale) on a row. I have the image paths in an array called images which looks similar to this,

$images = array(
  'iamge1.png',
  'image2.png,
  'image3.png,
  'image4.png,
);

How can I achieve this? I have been scrathing around for a few hours now :(

  • 写回答

2条回答 默认 最新

  • duanmei4149 2011-04-15 14:18
    关注

    Sorry I don't have much time in writing you the full code, but the following should work:

    <ul>
        <li><?php
            foreach($items as $i=>$item){
                // ...write item...
                if(($i % 3)==0 && $i!=0){ // if multiple of 3 and not the first time..
                    ?></li><li><?php
                }
            }
        ?></li>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度