Tricky Problem

5 July 2012, 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 - plugins
Sandbox - - reciprocal-affection hope-for-the-future textpattern -
Tips textpattern textpattern textpattern meaningful-labor textpattern textpattern textpattern

Solution

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:

MonTueWedThuFriSatSun
About-------
Plugins-------
Sandbox-------
Tips-------

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">
	<th>About</th>
	<td data-vintage="0">-</td>
	...
	<td data-vintage="6">-</td>
</tr>

This is obtained with

<table>
<thead>
	<tr><th></th><th>Mon</th>...<th>Sun</th></tr>
</thead>
<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>
	</tr>
</txp:section_list>
</table>

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='2']=hope-for-the-future;
//tr[@data-release='about']/td[@data-vintage='3']=hope-for-the-future; ...

with

<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>
</txp:article_custom>

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!

File(s)