Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-08-14 03:29:25

coopersita
Member
Registered: 2005-09-28
Posts: 68
Website

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

#2 2008-08-14 05:08:34

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

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).


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#3 2008-08-14 15:55:41

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

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.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#4 2008-08-14 16:12:45

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,453
Website GitHub

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

Online

#5 2008-08-14 17:39:46

renobird
Member
From: Gainesville, Florida
Registered: 2005-03-02
Posts: 786
Website

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

#6 2008-08-14 17:42:29

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: Let users sort articles

But then is this a large enough function that it should really be it’s very own pluign?


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#7 2008-08-14 17:55:23

renobird
Member
From: Gainesville, Florida
Registered: 2005-03-02
Posts: 786
Website

Re: Let users sort articles

There’s also tablekit for Prototype if that’s more your flavor. Although jQuery is already alongside TXP

Last edited by renobird (2008-08-14 17:55:34)

Offline

#8 2008-08-14 19:59:59

coopersita
Member
Registered: 2005-09-28
Posts: 68
Website

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

#9 2008-08-14 23:19:26

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,453
Website GitHub

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

Online

#10 2008-08-15 00:14:06

coopersita
Member
Registered: 2005-09-28
Posts: 68
Website

Re: Let users sort articles

Thanks all for your help.

I’ll give it a try.

Offline

#11 2008-08-15 01:25:37

coopersita
Member
Registered: 2005-09-28
Posts: 68
Website

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

Board footer

Powered by FluxBB