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

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


Joined: 22 Sep 2005
Posts: 2668
Location: Germany

Post How to filter news based on an XField Reply with quote
With the help of this code you can show news filtered by an XField. For example, show only news where the value of XField "mood" equals "happy".
Code:
$xfieldfilter = array('mood', '=', 'happy');



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


to this:
Code:
if(isset($xfieldfilter)){
function xfield_filter($id){
global $xfieldfilter;

   $return = true;
   $path_to_xfields = '../data/xfields-data.txt';
   $xfield_data = file($path_to_xfields);
   foreach($xfield_data as $line){
      $line_arr = explode("|>|", $line);
      if($line_arr[0] == $id){
         $all_xfields = explode("||", $line_arr[1]);
         foreach($all_xfields as $single_xfield){
            $my_xfield = explode("|", $single_xfield);
            if($my_xfield[0] == $xfieldfilter[0]){
               if($xfieldfilter[1] == '='){
                  if(trim($my_xfield[1]) == $xfieldfilter[2]){
                     $return = false;
                  }
               }
               if($xfieldfilter[1] == '<'){
                  if(trim($my_xfield[1]) < $xfieldfilter[2]){
                     $return = false;
                  }
               }
               if($xfieldfilter[1] == '>'){
                  if(trim($my_xfield[1]) > $xfieldfilter[2]){
                     $return = false;
                  }
               }
               if($xfieldfilter[1] == '<='){
                  if(trim($my_xfield[1]) <= $xfieldfilter[2]){
                     $return = false;
                  }
               }
               if($xfieldfilter[1] == '>='){
                  if(trim($my_xfield[1]) >= $xfieldfilter[2]){
                     $return = false;
                  }
               }
            }
            else{
               continue;
            }
         }
      }
      else{
         continue;
      }
   }
   
   return $return;

}
}

foreach ($query as $bg => $row){

if(isset($xfieldfilter)){
if(xfield_filter($row['id'])){
   continue;
}
}


How to use it:

Use something like this in your include code:
Code:
$xfieldfilter = array('my_xfield', '>', '5');


This will only show news which have an xfield with a value bigger than 5.

"my_xfield" is the name of the xfield, 5 is the value of the xfield and '>' is the operator. You can use "<", ">", "<=", ">=" or "=" as operators. Of course, if you don't have numbers in your xfield only "=" will work.
Fri Jun 20, 2008 1:48 pm View user's profile Send private message
Guest








Please login to hide the ads.

scottdallas



Joined: 04 May 2006
Posts: 1802
Location: US

Post Reply with quote
I know I'm behind but I have to say that that is brilliant and awesome fidd Smile I can think of lots of wonderful ways to use that.

_________________
www.scottdizzle.com uses cnr Smile
last update: 07-22-08: 8:30 pm
Tue Jul 08, 2008 6:03 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.