 |
 |
 |
 |
 |
| Author |
Message |
FI-DD
Admin

Joined: 22 Sep 2005 Posts: 2973 Location: Germany |
|
How to filter news based on an XField |
|
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".
 |  | $xfieldfilter = array('mood', '=', 'happy'); |
In inc/show.news.php change this:
 |  | foreach ($query as $bg => $row){ |
to this:
 |  | 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:
 |  | $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 |
|
 |
Guest
|
|
|
Please login to hide the ads.
|
|
|
|
 |
scottdallas

Joined: 04 May 2006 Posts: 2214 Location: US |
|
|
| Tue Jul 08, 2008 6:03 pm |
|
 |
|
|
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
|
|
 |
 |
 |
|