Tricky Problem

2012-07-05 16:41

Keywords: sort, table

Seen on this TXP Forum thread. In our case, we output a table of the (latest) published articles categories by section/day of the week, but the construction can be adapted to any data:

Mon Tue Wed Thu Fri Sat Sun
About - - hope-for-the-future hope-for-the-future - - -
Plugins textpattern textpattern textpattern textpattern textpattern - textpattern
Sandbox - - reciprocal-affection textpattern textpattern -
Tips textpattern textpattern textpattern meaningful-labor textpattern textpattern textpattern


The solution proposed on the forum is rather messy (and does not work with recent etc_query), we give here a simpler version. The idea is to first construct a table of all possible cases filled with -, and then replace the appropriate cases with articles categories.

So we start with an empty table:


If you take a look at the page source, every row of this table is marked up with data-release and data-vintage attributes:

<tr data-release="about">
	<td data-vintage="0">-</td>
	<td data-vintage="6">-</td>

This is obtained with

<txp:section_list wraptag="tbody" break="">
	<tr data-release="<txp:section />">
		<th><txp:section title="1" /></th>
		<td data-vintage="0">-</td>
		<td data-vintage="6">-</td>

Now we pass this table (stored in some <txp:variable name="table" />) to etc_query to replace the content of the cases corresponding to some article with its category. To this end, we construct a ;-separated list of replacement rules like

//tr[@data-release='about']/td[@data-vintage='3']=hope-for-the-future; ...


<txp:article_custom sort="Section, DAYOFWEEK(Posted)">
	<txp:if_different>//tr[@data-release='<txp:section />']/td[@data-vintage='<txp:posted format="%w" />']=<txp:category1 />;</txp:if_different>

and store it in <txp:variable name="replace" />. Now just call

<txp:etc_query data='<txp:variable name="table" />' replace='<txp:variable name="replace" />' />

and enjoy the result!