Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Let users sort articles
I know you can set the order of articles to be displayed in the txp:article or tx:article_custom tag, but is there a way to let the user sort the articles themselves?
I have a catalogue of products, and I’d like users to be able to sort the product list by name (title), date (posted) or price (custom field) via a link or button. Is that possible?
I looked into plugins to do this, but couldn’t find anything…
Thanks
Offline
Re: Let users sort articles
Hi coopersita,
you can try chs_if_urlvar or smd_if. This will let you do some conditional magic on your page templates depending on the variables you passed through the URL (like ?sort=price-asc
or ?sort=posted-desc
).
Like:
<txp:chs_if_urlvar name="sort" value="price-asc">
<txp:article sort="custom1 asc" />
</txp:chs_if_urlvar>
or
<txp:chs_if_urlvar name="sort" value="post-desc">
<txp:article sort="post desc" />
</txp:chs_if_urlvar>
(when sorting by custom field, you have to use the custom field internal name (custom1, custom2, etc) and not the name you gave to it).
Offline
Re: Let users sort articles
Would it be possible to pass the url var to the txp:article tag?
<txp:article sort="<txp:someTagToGetUrlVariable />" />
Although you’d probably want to validate it first so maybe it’s a crazier idea than I originally thought.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: Let users sort articles
MattD wrote:
Would it be possible to pass the url var to the txp:article tag?
I was thinking about that too — especially under TXP 4.0.7 where you could embed tags directly, it could be a good candidate for using <txp:variable />
— but, as you say, validation is important.
To that end, I have some ideas about extending smd_if to help handle multiple values (a new operator) which will save having to repeat the same variable name multiple times. For this type of situation I would like smd_if to be able to work like this:
// Set a default sort order
<txp:variable name="sort_order" value="price asc" />
// See if the urlvar has any valid sort values
<txp:smd_if field="urlvar:sortby" operator="in" value="price asc/price desc/post asc/post desc/id asc/id desc">
// Yes, then assign the sort to the value given in the URL
<txp:variable name="sort_order" value='<txp:php>echo $_GET["sortby"];</txp:php>' />
</txp:smd_if />
<txp:article sort='<txp_variable name="sort_order" />' />
Convoluted? Maybe. Powerful? Perhaps.
There might be a better way to handle the assignment (I have ideas about that too, since smd_if is a container tag) but I think that sort of structure might well work. If I can make smd_if behave that way (a big if, it’s only theory right now) it opens up some much better possibilities for conditional testing.
Of course, it doesn’t help coopersita right now unfortunately…
Last edited by Bloke (2008-08-14 16:13:40)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Let users sort articles
If you used a table to list the articles (proper use of a table) – then you might be able to use
jQuery tablesorter I’m not 100% sure this is what you are after, but I’m working on something similar myself – and have been experimenting with this plugin – it’s pretty sweet.
Offline
Re: Let users sort articles
But then is this a large enough function that it should really be it’s very own pluign?
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Offline
Re: Let users sort articles
The problem is that the catalogue is not in a table. It’s like in most product catalogues, in a grid (each product with its info) in a square…
Bloke’s solution is more of what I’m looking for… Couldn’t I just hard code it with php? Something like:
<txp:php>
switch ($_GET["sortby"]) {
case "date":
$sortby = "post asc";
break;
case "custom1":
$sortby = "custom1 asc";
break;
default:
$sortby = "title asc";
}
</txp:php>
<txp:article sort_order='<txp:php>echo $sortby;</txp:php>' />
What do you think?
Last edited by coopersita (2008-08-14 20:09:22)
Offline
Re: Let users sort articles
coopersita wrote:
Bloke’s solution is more of what I’m looking for… Couldn’t I just hard code it with php?
Yup, your solution would work fine and is more efficient than a plugin in this instance. You’d just need to make $sortby
global in both hunks of <txp:php>
or it might forget the value :-)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Let users sort articles
Thanks all for your help.
I’ll give it a try.
Offline
Re: Let users sort articles
It seems to work, but now my problem is that the sort function treats all custom fields as strings, so when I try to sort my products by price, 12.00 comes before 8.00 (because it starts with 1)…
Is there a way to tell the sort to treat them as numbers?
Offline
Pages: 1