Search code examples
sortingcontao

Sorting of news in contao


I am using "contao-news-sorting" module from github for sorting my news based on a rank value. This rank field is extended in news dca. I am able to sort it in ascending order. But the problem is, when the rank field is empty. It by default take the empty value as zero and it gets displayed at first. I actually need such empty values to get displayed at bottom. What can I do for that?. My code is

 public function fetchrankItems($newsArchives, $blnFeatured, $limit, $offset, $objModule) {

    $newsobject=  \NewsModel::findPublishedByPids($newsArchives, $blnFeatured, $limit, $offset);

    $t = \NewsModel::getTable();        
    $arrOptions = array();
    switch ($objModule->news_sorting)
    {
        case 'sort_rankid_asc':
            $arrOptions['order'] = "$t.rankid ASC";                
            break;
        case 'sort_random':
            $arrOptions['order'] = "RAND()";
            break;
        default:
            $arrOptions['order'] = "$t.date DESC";
    }
      return \NewsModel::findPublishedByPids($newsArchives, $blnFeatured, $limit, $offset, $arrOptions);       


}

Solution

  • This is more of a MySQL question, than it is a Contao question. See MySQL: Order by field, placing empty cells at end for instance.

    You can try the following for example:

    $arrOptions['order'] = "IF ($t.rankid <> '', 0, 1)";