Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-01-05 14:30:41

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,356
GitHub

[solved] Article category count for accurate descriptions

I’d appreciate an additional pair of eyes on the following, any advice is most welcome and gratefully received, especially if it saves me some work and reduces the work that Textpattern has to do.

Consider the following 4 article categories:

  • apple
  • banana
  • cherry
  • damson

A section has articles, displayed using an article list, and each article has zero, one or two of the above categories. I’d like to correctly describe the article list depending on what is being viewed. For example, where an article list has 4x articles categorised with ‘apple’ and 1x with ‘banana’:

apples and banana

Where there are more than two categories, I want to use commas and still be grammatically correct, like this:

apples, banana and damsons

…or the full-house:

apples, bananas, cherries and damsons

I’ve figured out how to display each fruit with one and more-than-one articles, and also to omit it where there are zero articles; that’s pretty straightforward. With my four categories, each with 3 states (zero articles, one article, more than one articles), I have 3×3×3×3 potential outcomes, giving me 81 things to take into account. I can, with lots of copy + pasting, build a large, nested query tree to cover all 81 eventualities. It’s likely to be a big block of markup, and I’m comfortable writing it, but is there a more efficient way that I could use instead?

Last edited by gaekwad (2014-01-07 20:31:44)

Offline

#2 2014-01-05 15:05:35

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: [solved] Article category count for accurate descriptions

Hi Pete,

if you’ve

figured out how to display each fruit with one and more-than-one articles, and also to omit it where there are zero articles

the rest is pretty straightforward:

<txp:variable name="cocktail" value='your_apple_item, banana_item, cherry_item, damson_item' />
<txp:php>
	if($items = array_filter(do_list(parse('<txp:variable name="cocktail" />')))) {
		$end = array_pop($items);
		echo $items ? implode(', ', $items).' and '.$end : $end;
	}
</txp:php>

Edit: shortened the code.

Edit: a one-liner, just for fun:

<txp:variable name="cocktail" value='your_apple_item, banana_item, cherry_item, damson_item' />
<txp:etc_query data="{?cocktail||do_list.array_filter.implode(, |$).strrev.explode( ,|$|2).implode( dna |$).strrev}" />

Last edited by etc (2014-01-05 16:32:51)

Offline

#3 2014-01-05 15:08:47

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,356
GitHub

Re: [solved] Article category count for accurate descriptions

Oleg, thank you very much. You’ve made a cold and weary Englishman much happier.

Note to self: learn PHP.

Offline

Board footer

Powered by FluxBB