Friday, June 24, 2011

Circularly stepping through query, array or array of arrays using mod

Let's say you have a menu with a "next" link and would like to step through the menu in a way that when you're on the last item, clicking next will bring you back to the top.  You can use mod, instead of if/else and, in most cases, have everything on only one line.

Stepping through a list of patterns returned by a query:
request.next = "?pattern=" & q_patterns.id[listfindnocase(valuelist(q_patterns.id),url.pattern) mod q_patterns.recordcount + 1]

Stepping through an array of keywords:
request.next = "?keywords=" & arrsearch[listfindnocase(arraytolist(arrsearch),url.keywords) mod arraylen(arrsearch) +1]

Stepping through an array of arrays of secondary categories:
for (i=1; i<=arraylen(arrcat); i++) {
     catposition = listfindnocase(arraytolist(arrcat[i]),url.secondary);
     if (catposition)
           request.next = "?secondary=" & arrcat[i][catposition mod arraylen(arrcat[i])+1]
}