How to filter news based on an XField

Frequently Asked Questions (read only)

How to filter news based on an XField

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

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: Select all
$xfieldfilter = array('mood', '=', 'happy');



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


to this:
Code: Select all
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: Select all
$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.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

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

I know I'm behind but I have to say that that is brilliant and awesome fidd :) I can think of lots of wonderful ways to use that.
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