【问题标题】:PHP foreach loop helpPHP foreach 循环帮助
【发布时间】:2011-04-26 13:57:53
【问题描述】:

希望有人能帮助我,

我的网站上有很多图片,看起来和这个类似,

$usp = array('/images/usp1.jpg',
             '/images/usp2.jpg', 
             '/images/usp3.jpg', 
             '/images/usp4.jpg', 
             '/images/usp5.jpg', 
             '/images/usp6.jpg'
            );

我有一个循环浏览我的产品的页面,并在li 中显示产品图片和一些详细信息。我想做的是从$usp数组中随机选择开始循环,然后显示4个产品,然后从$usp数组中放置另一个随机选择,然后显示另外4个产品,然后显示另一个随机选择来自$usp 数组。

本质上我想要这个效果

USP PRODUCT PRODUCT  
PRODUCT PRODUCT USP  
PRODUCT PRODUCT PRODUCT  
PRODUCT USP PRODUCT  
PRODUCT PRODUCT PRODUCT  
USP PRODUCT PRODUCT

目前这是我的循环。

   <?php if(count($product_sets) >= 1) : ?>
                <div class="clear clearfix productWrap" id="homeBestSellers">
                    <!-- <h3 class="label"><?php echo $category_details->categoryTitle; ?> <br> Product Sets</h3> -->

                    <ul class="clear clearfix productBoxes">
                    <?php
                    $number_of_blanks = (3 - (count($product_sets) % 3)); // 0, 1 or 2.
                    if ($number_of_blanks == 3) :
                        $number_of_blanks = 0;
                    endif;
                    $number_of_rows = ceil(count($product_sets) / 3);
                    $currentItem = 1;
                    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++;

                    }
                    if ($number_of_blanks > 0) :
                        ?>
                        <li><img src="<?php echo site_url('assets/img/blocks/phone-number.png'); ?>" alt="Phone Number" width="242" height="156"></li>
                        <?php
                    endif;
                    ?>
                    </ul><!-- #productSets -->
                </div><!-- #productWrap -->
                <?php endif; ?>

我怎样才能修改它以获得想要的效果?

【问题讨论】:

    标签: php codeigniter loops foreach modulus


    【解决方案1】:

    在循环顶部插入(粗略的概念,可能需要一些修改以适合您的代码):

    if ($currentItem % 4 == 0) {        // every fourth item
        $randIndex = array_rand($usp);  // pick random index from $usp
        $randUsp = $usp[$randIndex];
        // code to display $randUsp     // and display it in the table
    }
    

    编辑:正如@Ben 所指出的,您可能希望在使用时从$usp 中删除项目,或者跟踪哪些项目已经被退回,以便相同的项目如果这是一个潜在的问题,则不会多次返回。

    【讨论】:

    • 这可能会导致重复的$usp 项目被退回。
    【解决方案2】:
    shuffle($usp);
    foreach ($product_sets as $i => $product) {
        if ($i % 4 == 0) {
            list(, $randUsp) = each($usp);
            echo $randUsp;
        }
    
        …
    }
    

    这假设$product_sets 是一个连续数字索引的数组。否则,请使用一个单独的 $i = 0 来增加每一轮。

    【讨论】:

      【解决方案3】:

      如果您不希望任何 $usp 项目出现两次,您应该在 foreach 循环之前执行此操作:

      shuffle($usp);
      

      然后在你的 foreach 循环的顶部附近:

      if ($currentItem % 4 == 0) {
          $randusp = array_pop($usp);
          # output $randusp here...
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-06
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多