At first glance, it would seem a very simple matter to find out the number of records returned by a loop: just use the numeration tag: #TOTAL_BOUCLE
. But using this tag properly here is a little more tricky than at first thought.
Making sure we select all of the relevant messages
The first subtlety: we want all the forum message linked to the article, including replies to the messages, and replies to the replies , and so on, and so on...
An ordinary standard loop such as: <BOUCLE_forum(FORUMS) {id_article}>
selects only the messages that are replying directly to the article. To access the replies to those messages, we would normally just include a second loop inside the first one, but that’s not going to do the whole job for us in this case.
In order to be able to count all of the messages, we want the loop to select absolutely all the forum messages for that article, regardless of the depth of nesting that those messages might represent. To do that, we need a special criteria called {plat}
, meaning "flat" in French, which does exactly what it says and flattens the hierarchy as if it weren’t there. The criteria is applied as in: <BOUCLE_forum(FORUMS) {id_article} {plat}>
Display only the total
Now that we know we have all the records, we can look at how to count them. The trick here is to use the loop without displaying anything at all for each of the records! We don’t want the messages, nor their contents or titles, and we need to make sure that there aren’t even any spaces or carriage returns (otherwise the generated final HTML code will contain countless empty lines, wasteful and messy and quite unnecessary), which we do by eliminating everything from between the opening and closing tags of the loop as shown here: <BOUCLE_forum(FORUMS) {id_article} {plat}></BOUCLE_forum>
.
The interior of the loop displays strictly nothing at all, but we’d still like to be able to show the accumulated total number afterwards. The #TOTAL_BOUCLE
tag can be used not only inside the loop, but also (as in our case) in the conditional text that appears after the loop (text which would be shown after the loop provided it had some matching records) and in the alternative conditional text (text which is displayed if the loop was empty).
But there’s another subtlety to note here: the alternative conditional text will always display if the loop does not find any records; it is therefore displayed even if the loop does select some records (the forum messages in our case) but without actually having any displayable content within the loop tags.
We should therefore put our #TOTAL_BOUCLE
in the alternative conditional text.
If there are no messages at all attached to the article, #TOTAL_BOUCLE
will be empty, so we must not display the encapsulating message ("You have N forum message(s)") in this case.
<BOUCLE_howmany(FORUMS) {id_article} {plat}></BOUCLE_howmany>
[You have (#TOTAL_BOUCLE) message(s) in the forum.]
<//B_howmany>
One message, many messages...
We now need to display the number of messages, if there are any. But we would like to do better than that: we would like that the word "message" be automatically assigned a singular tense if the loop only finds a single reply, and otherwise use the plural if there are more than one.
To do this, we will use the test filters |=={...}
and |?{...,...}
to assign the correct word "message" or "messages" as is appropriate according to the #TOTAL_BOUCLE
tag:
[(#TOTAL_BOUCLE|=={1}|?{singular,plural})]
Which then gives us our final code:
<BOUCLE_howmany(FORUMS) {id_article} {plat}></BOUCLE_howmany>
[You have (#TOTAL_BOUCLE) [(#TOTAL_BOUCLE|=={1}|?{message,messages})] in the forum.]
<//B_howmany>