pdoResources - showing only the next elements

Hi,
I have a website where I publish articles. On the sub-page of a single article, I wanted to add a slider with links to the next articles. I am using pdoResources for this. Items are sorted by menuindex. And now the question, is it possible to easily set in pdoResources to make the slider show links only to the next articles?

Let’s say there are 20 articles on a page and the slider will have 5 elements.
Example 1: Page 1 article, so the slider will show items: 2,3,4,5,6
Example 2: Page 10 article, so the slider will shows items: 11,12,13,14,15
Example 3: Page 17 article, so the slider will show items: 18,19,20,1,2

My current code:
[[!pdoResources? &parents=[[*parent]] &resources=-[[*id]] &tpl=tpl_slider &limit=10 &depth=0 &sortby={“menuindex”:“ASC”}]]

Hey @ndcstudio

As a partial answer - I think you’ll want to use the offset parameter:

[[!pdoResources? 
&parents=`[[*parent]]`
&resources=`-[[*id]]`
&tpl=`tpl_slider` 
&limit=`10` 
&depth=`0` 
&sortby=`{“menuindex”:“ASC”}`
&offset=`10`
]]

This example will skip the first ten results and start at result 11.

However, you’ll need to programmatically determine where your current resource lies in the list of results.

If your menu indexes are sequential, like 1,2,3,4,5,6,7,8,9,10,11 etc then you could try offsetting by the current resource’s menuindex field:

&offset=`[[*menuindex]]`

If not - I think you may need to write a snippet to determine where the current resource lies in your list of resources.

Maybe you could use the where property. Something like this:

[[!pdoResources? 
	...
	&where=`{"menuindex:>": [[*menuindex]]}`
]]
1 Like

This solution is OK for the initial items (articles), but e.g. for item (article) 17 it only shows items 18,19,20 and is missing 1, 2 (i.e. from the beginning of the list) to display the whole slider (5 items). Do you have any idea how to solve th

I missed that part and didn’t notice that is has to wrap around for the last articles.

I suspect you have to write a custom snippet.
I don’t think you can achieve this with a single pdoResources call.

Hi,
I have written a snippet which does what I wanted, i.e. it selects the next resources and if the next elements are missing up to the set limit, it replenishes the previous ones. Maybe it will be useful for someone.

Name of the snippet: nextIdResources, which must have the parameter: id and may have:limit, sortby, order.

For example:
[[!nextIdResources? &id='[[*id]]' &limit='10']]

Snippet added to pdoResources:
[[!pdoResources? &parents='[[*parent]]' &resources='[[!nextIdResources? &id='[[*id]]' &limit=10]]' &tpl='tpl_slider' &depth='0' &sortby='' &sortdir='' &includeTVs='photo-main' &processTVs='1']]

Snippet code:

<?php

if($sortby == ''){$sortby="menuindex";}
if($order == ''){$order="ASC";}
if($limit == ''){$limit=5;}


$myResource = $modx->getObject("modResource", $id);
$criteria = $modx->newQuery("modResource");
$criteria->where([
    "modResource.parent IN (" . $myResource->get("parent") . ")",
    "modResource.published" => 1,
    "modResource.deleted" => 0,
]);
$criteria->select(["modResource.id"]);
$criteria->sortby($sortby, $order);

$arrayIds= array();
$resources = $modx->getCollection("modResource", $criteria);
$idAdd = false;
$counter = 0;

foreach ($resources as $resource) {
    if ($resource->get("id") == $id) {
        $idAdd = true;
    } else {
        if ($idAdd) {
            if($counter < $limit){
               array_push($arrayIds,$resource->get("id"));
               $counter++;
            }
        }
    }
}

//supplement
if($counter < $limit){
   foreach ($resources as $resource) {
      if ($counter < $limit && $resource->get("id") != $id && !in_array($resource->get("id"), $arrayIds) ) {
          array_push($arrayIds,$resource->get("id"));
      } 
   }
}

$result='';
foreach ($arrayIds as $id) {$result.= $id.',';}
$result = substr_replace($result ,"", -1);
return $result;
1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.