{"id":21,"date":"2008-08-03T06:53:34","date_gmt":"2008-08-03T12:53:34","guid":{"rendered":"http:\/\/bililite.nfshost.com\/blog\/index.php\/2008\/08\/03\/organizing-the-shiur-archives\/"},"modified":"2008-08-14T21:19:39","modified_gmt":"2008-08-15T03:19:39","slug":"organizing-the-shiur-archives","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2008\/08\/03\/organizing-the-shiur-archives\/","title":{"rendered":"Organizing the Shiur Archives"},"content":{"rendered":"I <a href=\"http:\/\/bililite.nfshost.com\/blog\/index.php\/2007\/12\/08\/the-new-young-israel-site\/\">was trying to figure out<\/a> how to organize the <a href=\"http:\/\/youngisrael-stl.org\/articlereader.php?archive=yes\">shiurim<\/a> in a way that allowed for \"AND\" filters; something like <code>author=shulman&amp;tag=audio&amp;tanach<\/code>. Regular expressions won't work; but PHP allows you to use <code>[]<\/code> after a name and it automatically creates an array, so <code>author=shulman&amp;tag[]=audio&amp;tag[]=tanach<\/code> works (<code>tag=audio&amp;tag=tanach<\/code> just overwrites the first <code>tag<\/code>).<br \/>\r\n\r\nThe archive filter is now basically:\r\n<pre><code class=\"language-php\">function shiurquery($get){\r\n  $ret = '1';\r\n  foreach($get as $query=&gt;$item) $ret .= shiurquerystring($item, $query.\" REGEXP\");\r\n  return $ret;\r\n}\r\n\r\nfunction shiurquerystring ($item, $query){\r\n  if (is_array($item))\r\n    return join (' AND ', array_map ('shiurquerystring', $item, array_fill(0, count($item), $query)));\r\n  return \"$query\\\"$item\\\"\";\r\n}<\/code><\/pre>\r\n\r\nWhere <code>shiurquerystring<\/code> uses <code>array_map<\/code> to recursively analyze each element of the array.\r\nIt's a bit obscure but works, and tighter than a for-each loop.<br \/>\r\n\r\nSo <code>shiurquerystring(\"shulman\", \"author REGEXP\",)<\/code> returns <code>'author REGEXP \"shulman\"'<\/code> and\r\n<code><br \/>\r\nshiurquerystring(array(\"Tanach\", \"Audio\"), \"tag REGEXP\")<\/code> returns <code>'tag REGEXP \"Tanach\" AND tag REGEXP \"Audio\"'<\/code>,<br \/>\r\njust as we wanted, <br \/>and\r\n\r\n<code>shuirquery(parse_str(\"author=shulman&amp;tag[]=Audio&amp;tag[]=Tanach\")<\/code> returns<br \/>\r\n<code>'1 AND author REGEXP \"shulman\" AND tag REGEXP \"Audio\" AND tag REGEXP \"Tanach'<\/code>.<br \/>\r\n\r\nPerfect!<br \/>\r\n\r\nThe \"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.","protected":false},"excerpt":{"rendered":"I was trying to figure out how to organize the shiurim in a way that allowed for \"AND\" filters; something like author=shulman&amp;tag=audio&amp;tanach. Regular expressions won't work; but PHP allows you to use [] after a name and it automatically creates an array, so author=shulman&amp;tag[]=audio&amp;tag[]=tanach works (tag=audio&amp;tag=tanach just overwrites the first tag). The archive filter is [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":2,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}