2011-04-26 187 views
1

希望有人能幫助我,PHP foreach循環的幫助

我有我的網站上的圖像陣列,它看起來與此類似,

$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; ?> 

我該如何修改這個以便獲得理想的效果?在圈頂部

回答

0

插入(粗略的概念,可能需要做一些修改,以適應您的代碼):

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刪除的項目,如他們被使用,或者跟蹤哪些已經被返回,所以如果這是潛在的問題,那麼同一個項目不會被多次返回。

+1

這可能會導致重複返回的$ usp項目。 – Ben 2011-04-26 14:57:00

0
shuffle($usp); 
foreach ($product_sets as $i => $product) { 
    if ($i % 4 == 0) { 
     list(, $randUsp) = each($usp); 
     echo $randUsp; 
    } 

    … 
} 

這裏假設$product_sets是一個連續的數字索引數組。否則使用每轉一圈的單獨$i = 0

0

如果您不希望出現兩次,您應該在foreach循環之前做到這一點的任何$ USP項目:

shuffle($usp); 

然後靠近你的foreach循環的頂部:

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