How to sort news based on an XField

Frequently Asked Questions (read only)

How to sort news based on an XField

Postby FI-DD » Fri Jun 20, 2008 1:53 pm

With the help of this code you can sort your news based on the value of an XField.

In inc/show.news.php find this:
Code: Select all
foreach ($query as $bg => $row){


and add above:
Code: Select all
if (isset($sortbyxfield)){

   $path_to_xfields = '../data/xfields-data.txt';
   
   foreach (file($path_to_xfields) as $name => $value) {
      list($id, $xfieldsdata) = explode("|>|", trim($value), 2);
      $xfieldsdata = explode("||", $xfieldsdata);
      foreach ($xfieldsdata as $xfielddata) {
         list($xfielddataname, $xfielddatavalue) = explode("|", $xfielddata);
         $xfields_data[$id][$xfielddataname] = $xfielddatavalue;
      }
   }

   if (!function_exists("sortbyxfield")) {
      function sortbyxfield($a, $b) {
         global $xfields_data, $sortbyxfield;
         

         if ($xfields_data[$a['id']][$sortbyxfield[0]] == $xfields_data[$b['id']][$sortbyxfield[0]]){
            return 0;
         }
         else{
            if(strtolower($sortbyxfield[1]) == 'asc'){
               return ($xfields_data[$a['id']][$sortbyxfield[0]] < $xfields_data[$b['id']][$sortbyxfield[0]]) ? -1 : 1;
            }
            else{
               return ($xfields_data[$a['id']][$sortbyxfield[0]] < $xfields_data[$b['id']][$sortbyxfield[0]]) ? 1 : -1;
            }
         }
      }
   }

   usort($query, 'sortbyxfield');
}


How to use it:

Add something like this to your include code:
Code: Select all
$sortbyxfield = array('test', 'ASC');


"test" is the name of the XField and "ASC" is the order. You can show news in an ascending order ("ASC") or in a descending order ("DESC").
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Tue Jul 08, 2008 6:01 pm

I know this is probably a stupid question but I can't think of many scenarios where you'd want to sort by an XField and I am confused about whether or not it sorts alphabetically when you use ASC and DESC?

In other words, could someone provide an scenario where this would be useful?
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby FI-DD » Thu Jul 10, 2008 6:20 pm

If you have an XField called "sticky" with the values 0 (not sticky) or 1 (sticky) you can show the sticky articles first when sorting by XField "sticky".

Or let's say you have an XField called "priority" with values from 1 to 5. So now you can show the most important articles first.

I think there are many scenarios where this hack might be usefull.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Thu Jul 10, 2008 6:55 pm

That makes sense, thank you... got my gears goin ;) two very good examples
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US


Return to FAQ



Who is online

Users browsing this forum: No registered users and 0 guests

cron