I
was trying to figure out how to organize the
shiurim in a way that allowed for "AND" filters; something like
author=shulman&tag=audio&tanach
. Regular expressions won't work; but PHP allows you to use
[]
after a name and it automatically creates an array, so
author=shulman&tag[]=audio&tag[]=tanach
works (
tag=audio&tag=tanach
just overwrites the first
tag
).
The archive filter is now basically:
function shiurquery($get){
$ret = '1';
foreach($get as $query=>$item) $ret .= shiurquerystring($item, $query." REGEXP");
return $ret;
}
function shiurquerystring ($item, $query){
if (is_array($item))
return join (' AND ', array_map ('shiurquerystring', $item, array_fill(0, count($item), $query)));
return "$query\"$item\"";
}
Where
shiurquerystring
uses
array_map
to recursively analyze each element of the array.
It's a bit obscure but works, and tighter than a for-each loop.
So
shiurquerystring("shulman", "author REGEXP",)
returns
'author REGEXP "shulman"'
and
shiurquerystring(array("Tanach", "Audio"), "tag REGEXP")
returns
'tag REGEXP "Tanach" AND tag REGEXP "Audio"'
,
just as we wanted,
and
shuirquery(parse_str("author=shulman&tag[]=Audio&tag[]=Tanach")
returns
'1 AND author REGEXP "shulman" AND tag REGEXP "Audio" AND tag REGEXP "Tanach'
.
Perfect!
The "1 AND" at the beginning makes the coding easier. The actual code is a bit more involved, to prevent cross-site scripting and to limit the filters to specific items.