Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-08-25 14:41:40

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,706
Website

Custom category order of linklists using "breakby" method? [resolved]

Say I want to output a linklist grouped by category. I could loop over txp:category_list and call txp:linklist for each category in turn. If I enter:

<txp:category_list type="link" category="category-b, category-a" …

I get the linklist grouped by category in the order I write them in the categories attribute, because category_list processes each category in the order you supply them. So far so good, but this method results in a fresh db query for each category, e.g. if you have 6 categories, you end up with 6 db queries.

I can also do:

<txp:linklist category="category-b, category-a" sort="category, linksort" breakby="<txp:link_category />" breakform="link_breakby_item" form="link_item"…

with the breakby attribute calling the breakform each time the category changes so that it can output a category heading. This requires only 1 db query, but to avoid new category headings appearing every time a link has a different category to the last, the available links need to first be sorted by category. The tag above therefore outputs grouped blocks of links alphabetically, i.e. with category-a before category-b.

Can I add something in place of category asc or category desc to use the same order as supplied in the category attribute?

*While writing this, I realised I could probably use esq_sectionsort to get around this, but maybe there’s an in-built way.


TXP Builders – finely-crafted code, design and txp

Offline

#2 2024-08-25 15:24:49

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

Re: Custom category order of linklists using "breakby" method? [resolved]

jakob wrote #337710:

Can I add something in place of category asc or category desc to use the same order as supplied in the category attribute?

Yes. Pretty sure you can use the MySQL FIELD function. E.g.:

<txp:category_list sort="FIELD(category, 'category-b', 'category-a')" ...

But the downside is you have to repeat yourself unless (maybe) you store the list of cats in a variable and inject that twice into the tag, individually quoting the one in the FIELD() function.

Edit: although not for Links, there’s an example here as well.

Last edited by Bloke (2024-08-25 15:31:54)


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

#3 2024-08-25 16:21:31

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,706
Website

Re: Custom category order of linklists using "breakby" method? [resolved]

ACE! Now I see it, I remember it. Thank you!

Fortunately, the comma-separated category names are stored in a txp:variable, so I can get the individually quoted variant of that with:

<txp:variable name="linkcats" breakby="," escape="trim, quote" break="," />

(gotta love Oleg’s successive improvements to the attributes. Must also thank lindabb for their question in the other thread: it reminded me of the existence of the escape="quote" option)

That also means the same variable can be used twice in the tag…

<txp:linklist category='<txp:variable name="linkcats" />' 
     sort='FIELD(category, <txp:variable name="linkcats" breakby="," escape="trim, quote" break="," />), linksort'
    …

Nice!


TXP Builders – finely-crafted code, design and txp

Offline

#4 2024-08-25 16:37:17

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

Re: Custom category order of linklists using "breakby" method? [resolved]

Glad that worked. The FIELD() function is really handy at times.

Unless things have changed in recent versions of PHP, it’s one of the things we’ll really miss when we eventually move to PDO, because it’s not supported across database vendors. We would have to drop it or be forced to only support MySQL derivatives, which negates some of the benefits of using PDO, beyond parameterized queries.


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 2024-08-25 16:41:58

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,706
Website

Re: Custom category order of linklists using "breakby" method? [resolved]

I didn’t know that. I was thinking of mentioning PDO in the TXP 5 thread, but wasn’t sure whether it was a priority… I didn’t know it was on the cards anyway.

Maybe TXP could then have a corresponding order “keyword” (custom?) for the user-supplied original order that we could use in place of asc or desc and that then translates into the corresponding PDO equivalent.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2024-08-25 17:05:09

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

Re: Custom category order of linklists using "breakby" method? [resolved]

I don’t think some of the DB engines support that kind of thing full stop. They pick and choose which bits of the SQL 92 standard to stick to, and which bits to customise/extend.

We would probably end up having to do the custom ordering in PHP. Bit of a bind.

It really depends what direction we take with PDO and an ORM (or not) and whether we think supporting other DB vendors is a priority. Our database layer between PDO and Txp can then either use some third-party like Doctrine DBAL or our own layer, or a combination. See the notes in the small CMS thread.


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

#7 2024-08-25 19:09:42

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,706
Website

Re: Custom category order of linklists using "breakby" method? [resolved]

Thanks. For some reason I completely missed the rest of that thread, so I had no idea it had already been discussed.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB