PHP foreach 循环帮助

发布于 2024-11-03 20:30:20 字数 3170 浏览 3 评论 0原文

希望有人可以帮助我,

我的网站上有一系列图像,看起来与此类似,

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

我该如何修改它才能达到预期的效果?

Hoping someone can help me,

I have array of images on my website, it looks similar to this,

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

I have a page the loops through my products and displays a product image and some details in an li. What I am wanting to do it start the loop off with a random selection from the $usp array and then show 4 products, then place another random selection from the $usp array, then show another 4 products and then show another random selection from the $usp array.

In essence I want this effect

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

Currently this is the loop I have.

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

How can I modify this so that I have the desired effect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戒ㄋ 2024-11-10 20:30:20

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

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 当它们被使用时,或者跟踪哪些已经被退回,这样如果这是一个潜在的问题,同一个项目就不会被多次退回。

Insert at top of loop (rough concept, may need some modification to fit your code):

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
}

Edit: As pointed out by @Ben, you may want to remove items from $usp as they are used, or keep track of which ones have already been returned, so that the same item isn't returned multiple times if that's a potential problem.

去了角落 2024-11-10 20:30:20
shuffle($usp);
foreach ($product_sets as $i => $product) {
    if ($i % 4 == 0) {
        list(, $randUsp) = each($usp);
        echo $randUsp;
    }

    …
}

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

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

    …
}

This assumes $product_sets is a continuously numerically indexed array. Otherwise use a separate $i = 0 that you increment each turn.

倒带 2024-11-10 20:30:20

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

shuffle($usp);

然后在 foreach 循环顶部附近执行此操作:

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

If you don't want any $usp items from appearing twice you should do this before your foreach loop:

shuffle($usp);

And then near the top of your foreach loop:

if ($currentItem % 4 == 0) {
    $randusp = array_pop($usp);
    # output $randusp here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文