Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-03-03 15:42:36

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,907
Website GitHub

filter articles according to both categories?

If I assign each article two categories, in this case category1 representing type and category2 representing location, what would I enter into the address-line to filter by both?

I can do www.mysite.com/animals?c=birds to list all birds anywhere and www.mysite.com/animals?c=england to list all animals in england, but what does one enter to filter for all birds in england?

I know that chh_article_custom can filter according to both but not how to link to or “call” this page. Is there some syntax I’ve overlooked or do I have to investigate using something like chs_ifurlvar or chs_cookie to pass information to a following page and then build different chh_article_custom tags depending upon the variable passed?

Any tips or alternative suggestions welcome!

jakob


TXP Builders – finely-crafted code, design and txp

Offline

#2 2006-03-05 18:02:39

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: filter articles according to both categories?

You may well have found a solution in the meantime, if so ignore this, but as Textpattern as far as I know doesn’t have a way to do this, my choice would be the chs_ifurlvar plugin.

Offline

#3 2006-03-05 19:16:23

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,907
Website GitHub

Re: filter articles according to both categories?

yes, with quite some experimentation, chh_article_custom, chs_ifurlvar and a tiny extra function to output the value of the GET var, I’ve cobbled together a two category filter that can be controlled through the URL according to the pattern:

URL + ?c1=___&?c2=___

with the help of 2 extra mod_rewrite rules in the htaccess I can simplify this to a ‘guessable’ clean url:

www.mysite.com/section/category/c1/c2

I use 2 drop-down (select) lists with select name=“c1” and select name=“c2” to create these urls and the function catches the different url combinations and filters accordingly.

1. Additional plug-in function which simply outputs the value of the requested urlvar, used later to output the searched for categories c1 and c2 (I installed chs_ifurlvar, went into the plugin code and added this function):

function chs_output_urlval($atts) {
$thevar = ($atts["var"]) ? $atts["var"] : "function";
return ($_GET[$thevar]) ? $_GET[$thevar] : "";
}

2. On the listpage, get the values of urlvars c1 and c2, test which are present and construct the filter-by category=“string” for chh_article_custom accordingly, then call chh_article_custom using this filter-by string:

<txp:php>
$chscat1=chs_output_urlval(array("var"=>"c1"));
$chscat2=chs_output_urlval(array("var"=>"c2"));
if ($chscat1 !=="" and $chscat2 !=="") {
    $chscat="$chscat1&$chscat2";
} elseif ($chscat1 !=="") {
    $chscat="$chscat1";
} elseif ($chscat2 !=="") {
    $chscat="$chscat2";
} else { 
    $chscat="?";
}

echo chh_article_custom(
    array(
        "section"=>"?",
        "category"=>"$chscat",
        "listform"=>"myform",
        "children"=>"y"
    )
);
</txp:php>

3. Additional mod_rewrite rules for …/category/c1/c2 clean urls, added after the other RewriteRule entries in the textpattern root directory .htaccess:

RewriteRule ^([^/]+)/category/([^/]+)/?$ index.php?s=$1&c=$2 [L]
RewriteRule ^([^/]+)/category/([^/]+)/([^/]+)/?$ index.php?s=$1&c1=$2&c2=$3 [L]

It’s not elegant code but it works (for me at least). I’m sure someone more savvy in php than I can tell me how to achieve this better or streamline the code and mod_rewrite rules. Theoretically if chh_article_custom could also ‘sense’ ?c1= and ?c2= like it can with ?c= then this would be unnecessary.

What does not (as yet) work is filtering by two categories when one of them is a parent-category. This is not (yet) a feature of chh_article_custom. To avoid getting no results, I only present the child categories as options in the select list.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB