Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
I try to randomly shuffle the items of a repeatable, but my approach does not work:

Code: Select all
<cms:repeatable name='photos'>
    <cms:editable name='photo'type='image' />
</cms:repeatable>

<div class="gallery">
    <cms:show_repeatable 'photos'>
    <cms:php>
        $photos = array('<img class="card" src="<cms:show photo />">');
    </cms:php>
    </cms:show_repeatable>
    <cms:php>
        shuffle($photos);
        foreach ($photos as $photo) {
            echo $photo;
        }
    </cms:php>
</div>


Any ideas, how I could solve it?
My next approach works better. $gallery is set, but explode returns an empty array.

Code: Select all
<div class="gallery">
    <cms:show_repeatable 'photos'>
    <cms:set gallery="<cms:show gallery />|<cms:show photo />" scope="global"/>
    </cms:show_repeatable>

    <cms:php>
        $gallery = substr("<cms:show gallery />",1);
        $photos = explode('|', $gallery);
        print_r($photos);
    </cms:php>
</div>


?!?
Okay, I had to set the type to string. This example works now.

Code: Select all
<div class="gallery">
    <cms:show_repeatable 'photos'>
    <cms:set gallery="<cms:show gallery />|<cms:show photo />" scope="global"/>
    </cms:show_repeatable>

    <cms:php>
        $gallery = substr("<cms:show gallery />",1);
        settype($gallery, "string");
        $photos = explode("|", $gallery);

        shuffle($photos);
        foreach ($photos as $photo) {
        echo "<img src=\"$photo\" class=\"card\" alt>";
        }
    </cms:php>
</div>
Your first approach would have worked if you added a global $photos; in both php blocks.
4 posts Page 1 of 1

Who is online

In total there are 9 users online :: 0 registered, 0 hidden and 9 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 9 guests