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
cron