Roman Sobol
Roman Sobol

Reputation: 75

Sorting in modx

Hello.

Have problem while sorting issues of journal in archive. CMS Modx. Code not mine. Now its looking like:

<li class="journals">
<ul class="ye">
<li class="year">2015</li>
<ul class="in_nums 2015" style="display: inherit;">
<li data-issue="4804">№1(48)</li>
<li data-issue="5314">№3(51)</li>
<li data-issue="4980">№2(50)</li>
<li data-issue="5148">№2(49)</li>
<li data-issue="5437">№3(52)</li>
</ul>
</ul>
</li>

As you see Issues is not in any type of sorting. Years sorting normally.

Snippet:

<?php
$ctx = $modx->getContext($modx->context->key);
$issue_con = $ctx->config['issue_con'];

$query = $modx->newQuery('modResource');
$query->select(array('pagetitle','id'));
$query->where(array('parent'=>$issue_con));
$query->prepare();
$query->stmt->execute();
$res = $query->stmt->fetchAll(PDO::FETCH_ASSOC);

$years = array();
foreach ($res as $r)
{
    $mas = explode(', ',$r['pagetitle']);
    $years[intval($mas[1])][$mas[0]] = $r['id'];
}
krsort($years);
$output = '<ul class="ye">';
foreach ($years as $y=>$nums)
{
    $output .= '<li class="year">'.$y.'</li><ul class="in_nums '.$y.'">';
                foreach ($nums as $num=>$pid)
                {
       $output .= '<li data-issue="'.$pid.'">'.$num.'</li>';
                }
                $output .= '</ul>';
}
$output .= '</ul>';

echo $output;

Need to sort issues from oldest to newest (like ksort I suppose) using numbers in brackets (№3(51)) or pids (4980).

Thanks in advance!

Upvotes: 1

Views: 94

Answers (1)

Sean Kimball
Sean Kimball

Reputation: 4494

You can do your sorting in your query, see the docs: https://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.sortby

to sort by the resourceid :

$query->sortby('id','ASC');

and remove the sorting by years

Upvotes: 2

Related Questions