We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Random item from a repeater and max amount based on item count

Hi Guys,

I have a template that has a repeater to upload an image. I want to choose only one of these images at random and display it. I have this working with the following code.

Template

~~ <perch:repeater id="images" label="Images" scope-parent="true"> <perch:if id="perch_item_index" match="eq" value="{landimg}" /> <img src="<perch:content id="image" type="image" width="1400" label="Image" help="Recommended that images must be at least 1400px wide and that you use square images" bucket="images" />"

                    srcset="
                    <perch:content id="image" type="image" width="1400" /> <perch:content id="image" type="image" width="1400" output="w" />w,
                    <perch:content id="image" type="image" width="1200" /> <perch:content id="image" type="image" width="1200" output="w" />w,
                    <perch:content id="image" type="image" width="800" /> <perch:content id="image" type="image" width="800" output="w" />w,
                    <perch:content id="image" type="image" width="600" /> <perch:content id="image" type="image" width="600" output="w" />w,
                    <perch:content id="image" type="image" width="350" /> <perch:content id="image" type="image" width="350" output="w" />w,
                "

                sizes="
                    (min-width: 68.750em) 33vw,
                    (min-width: 47.500em) 50vw,
                    100vw
                "

                alt="<perch:content id="parent.heading" />"

                class="section-teaser__image"
            >

</perch:if> </perch:repeater>

        <div class="section-teaser__content">
            <h2 class="section-teaser__title"><perch:content id="heading" type="smarttext" label="Heading" required="true" title="true" /></h2>
            <span class="section-teaser__subline"><perch:content id="subline" type="smarttext" label="Subline" required="true" title="true" />&hellip;</span>
            <p class="section-teaser__readmore">View Our Range</p>
        </div>

and PHP page
    <?php
        PerchSystem::set_var('landimg', rand(1, 2));
        perch_content_custom('Product Box  - Land', array(
            'template'=>'home/product_box.html'
        ));
    ?>

~~~

However, as there will be several of these boxes and each item might contain a varying amount of images I would like to check how many items each repeater has and then set this as the top amount as to not produce a random number that an image doesn't exist.

So my question is how can I check how many items are in the repeater and then change this to set the maximum value? PerchSystem::set_var('landimg', rand(1, MAXVLAUE));

I tried to use <perch:content id="perch_item_count" hidden="true" /> but I think this is only accessible inside the template and not outside so I cannot use it in my PHP code?

Any suggestions?

Thanks.

Terry Upton

Terry Upton 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

No, repeaters aren't really designed to be filtered like that.

Ok If I changed the setup to have a separate template and a multiitem region for just the images instead. Could I check the count and set the random max value that way?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You wouldn't need to check the count, you could just sort randomly using the provided sort options.