Forum

Thread tagged as: Problem

sort-order not working

I'm trying to get a list of images from a repeater in random order using perch_content_custom but changing the sort-order or count does nothing.

perch_content_custom('Slideshow', array(
   'page'=>'/index.php',
   'template'=>'slides.html', 
   'sort-order'=>'RAND',
   'count'=>3
));

Here's the slides.html template:

<perch:repeater id="slides" label="Slider images">
    <img src="<perch:content id="image" type="image" width="2200" height="1100" crop="true" quality="30" sharpen="2" />" alt="<perch:content id="alt" type="text" />" />
</perch:repeater>

All 7 slides are output in descending order, not 3. I tried adding 'sort'=>'image' and 'sort'=>'alt' to the parameter list but nothing is output at all when I add this in. Any ideas why this could be?

Diagnostics:

Perch: 2.8.24, PHP: 5.6.10, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (2.8.24), assets (2.8.24), categories (2.8.24), perch_blog (5.0), perch_forms (1.8.3)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_forms', );
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/adam/Sites/burtownhouse.ie/site/perch
PERCH_CORE: /Users/adam/Sites/burtownhouse.ie/site/perch/core
PERCH_RESFILEPATH: /Users/adam/Sites/burtownhouse.ie/site/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: dc1fef2ad0fcd9f943c02ebb43d85dbc
Resource folder writeable: Yes
HTTP_HOST: burtownhouse.dev
DOCUMENT_ROOT: /Users/adam/Sites/burtownhouse.ie/site
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Adam Green

Adam Green 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to set sort for sort-order to have any context. Could you try that?

That's what I assumed, though when I try that nothing is output at all:

perch_content_custom('Slideshow', array(
    'page'=>'/index.php',
        'template'=>'slides.html',
    'sort'=>'image',
    'sort-order'=>'DESC',
    'count'=>3
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorting on image will sort on its file path - is that what you're expecting?

If not, what are you expecting?

I'd like them output in random order so it shouldn't matter what id I sort them on - there are only 2 ids to choose from (image and alt), but when I try either ('sort'=>'image' or 'sort'=>'alt'), no images are output!

The last code excerpt was meant to be RAND, not DESC:

perch_content_custom('Slideshow', array(
    'page'=>'/index.php',
    'template'=>'slides.html',
    'sort'=>'image',
    'sort-order'=>'RAND',
    'count'=>3
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you remove the sorting, do the images show?

Yes, all 7 images show when 'sort'=>'image' is removed. 'count'=>3 doesn't appear to limit the number either.

perch_content_custom('Slideshow', array(
    'page'=>'/index.php',
    'template'=>'slides.html',
    'count'=>3
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you turn on debug for the page and add the sorting back in, what does it output?

Thanks Drew, here's the debug output:

Time    Δ   Debug Message
0.0398  0   SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Slideshow' AND (regionPage='/index.php' OR regionPage='*')
0.0403  0.0004  SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='image' WHERE ((idx.regionID=18 AND idx.itemRev=3)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval ORDER BY RAND() LIMIT 0, 3
0.0469  0.0066  Using template: /templates/content/slides.html

slides.php:

include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');
perch_content_custom('Slideshow', array(
    'page'=>'/index.php',
    'template'=>'slides.html',
    'sort'=>'image',
    'sort-order'=>'RAND',
    'count'=>3
));

slides.html template:

<perch:repeater id="slides">
    <perch:if exists="perch_item_first"><perch:else />
        <div class="dumbItem"><img src="<perch:content id="image" type="image" width="2200" height="1100" crop="true" quality="30" sharpen="2" />" alt="<perch:content id="alt" type="text"/>"/></div>
    </perch:if>
</perch:repeater>

This is the main slideshow.html template assigned to the 'Slideshow' content region in index.php

<perch:repeater id="slides" label="Slides" max="25">
    <perch:before><div class="dumbCrossFade"></perch:before>
        <perch:if exists="perch_item_first">
            <div class="dumbItem">
                <img src="<perch:content id="image" type="image" label="Slide image" width="2200" height="1100" crop="true" quality="30" sharpen="2" />" alt="<perch:content id="alt" type="text" label="Slide description" required="true" />" />
            </div>
        <perch:else /></perch:if>
    <perch:after></div></perch:after>
</perch:repeater>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ah, so your region contains only 1 item, and that 1 item has a repeater with all your images in?

Yep that's right. Is that wrong?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No wrong, but it's not going to do what you want in this case. Repeaters can't be sorted or filtered.

In this case it would be better to set the region to allow multiple items (in the region options) and then add one image per item, without a repeater.

Thanks Drew, that's it. Sorry I didn't cop that sooner!