Forum Index
RegisterSearchFAQMemberlistUsergroupsLog in
How to sort news based on an XField

 
Reply to topic    Forum Index » FAQ View previous topic
View next topic
How to sort news based on an XField
Author Message
FI-DD
Admin


Joined: 22 Sep 2005
Posts: 2736
Location: Germany

Post How to sort news based on an XField Reply with quote
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:
foreach ($query as $bg => $row){


and add above:
Code:
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:
$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").
Fri Jun 20, 2008 1:53 pm View user's profile Send private message
Guest








Please login to hide the ads.

scottdallas



Joined: 04 May 2006
Posts: 1843
Location: US

Post Reply with quote
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?

_________________
www.scottdizzle.com uses cnr Smile
last update: 07-22-08: 8:30 pm
Tue Jul 08, 2008 6:01 pm View user's profile Send private message Visit poster's website AIM Address
FI-DD
Admin


Joined: 22 Sep 2005
Posts: 2736
Location: Germany

Post Reply with quote
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.
Thu Jul 10, 2008 6:20 pm View user's profile Send private message
scottdallas



Joined: 04 May 2006
Posts: 1843
Location: US

Post Reply with quote
That makes sense, thank you... got my gears goin Wink two very good examples

_________________
www.scottdizzle.com uses cnr Smile
last update: 07-22-08: 8:30 pm
Thu Jul 10, 2008 6:55 pm View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:    
Reply to topic    Forum Index » FAQ All times are GMT + 1 Hour
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum



Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Music Lyrics.