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

Joined: 22 Sep 2005 Posts: 2736 Location: Germany |
|
How to sort news based on an XField |
|
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:
 |  | foreach ($query as $bg => $row){ |
and add above:
 |  | 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:
 |  | $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 |
|
 |
Guest
|
|
|
Please login to hide the ads.
|
|
|
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
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
last update: 07-22-08: 8:30 pm |
|
| Tue Jul 08, 2008 6:01 pm |
|
 |
FI-DD
Admin

Joined: 22 Sep 2005 Posts: 2736 Location: Germany |
|
|
|
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 |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
That makes sense, thank you... got my gears goin two very good examples
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Thu Jul 10, 2008 6:55 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
|
|
 |
 |
 |
|