0

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);       


}
Ques
  • 253
  • 4
  • 19

1 Answers1

2

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)";
fritzmg
  • 2,494
  • 3
  • 21
  • 51
  • whether this is the way to write? Am getting sql error. $arrOptions['order'] = "$t.if(`rankid` <> '', 0, 1) ASC"; – Ques Nov 09 '17 at 11:13
  • 1
    Your syntax is wrong. It should be `"IF ($t.rankid <> '', 0, 1)"`. – fritzmg Nov 09 '17 at 11:28
  • Whether this query will not work with "contao-news-sorting" module?? For me, it is not working. – Ques Nov 09 '17 at 11:55
  • Tried with $arrOptions['order'] = "$t.IF ($t.rankid <> '', 0, 1) ASC"; – Ques Nov 09 '17 at 12:01
  • Why are you writing `$t.IF`? That makes no sense ;). Your final query would then look like this: `… ORDER BY tl_news.IF (tl_news.rankid <> '', 0, 1) ASC`, which is obviously wrong. – fritzmg Nov 09 '17 at 12:44
  • 1
    The above query was bit confused for me. So I wrote in this way and it worked. $arrOptions['order'] = "$t.rankid = 0, $t.rankid, $t.date DESC"; – Ques Nov 10 '17 at 05:30