Forum
Multiple filter for months
Hello,
I've set up a filter to filter by month. Most of it comes from https://grabapipit.com/blog/user-filtered-product-listing (Thank you, Hussein)
My filter.html template:
<perch:form id="filters" method="get">
<fieldset>
<legend>Maand</legend>
<ul>
<perch:input id="eventDateTime" type="radio" wrap="li" options="januari,februari,maart,april,mei,juni,juli,augustus,september,oktober,november,december" class="radio" <perch:if exists="selected_maand">value="<perch:forms id="selected_maand" />"</perch:if> />
</ul>
</fieldset>
<perch:forms id="cats_categorie" html="true" />
<perch:forms id="cats_genre" html="true" />
<div>
<input type="submit" value="Apply">
<a href="<perch:forms id="perch_page_path" />">Clear</a>
</div>
</perch:form>
In my listing:
/* ---------- MONTH ---------- */
$januari = "2018-01-01, 2018-01-31";
$februari = "2018-02-01, 2018-02-28";
$maart = "2018-03-01, 2018-03-31";
$april = "2018-04-01, 2018-04-30";
$mei = "2018-05-01, 2018-05-31";
$juni = "2018-06-01, 2018-06-30";
$juli = "2018-07-01, 2018-07-31";
$augustus = "2018-08-01, 2018-08-31";
$september = "2018-09-01, 2018-09-30";
$oktober = "2018-10-01, 2018-10-31";
$november = "2018-11-01, 2018-11-30";
$december = "2018-12-01, 2018-12-31";
if(perch_get('eventDateTime')) {
$maand = perch_get('eventDateTime');
// filter for greater or equal to
$filters[] = [
'filter' => 'eventDateTime',
'match' => 'between',
'value' => $$maand,
];
}
I know that it is not the cleanest code, but it works for now. My problem is that I am now using radio buttons, but I would like to be able to select multiple months, so using checkboxes. But I am not sure how to do this. What would be a solution for this?
Thanks, Mike
You'd append to
$filters[]
for each of the filters you want to add.Hello Mike,
Thanks for the mention.
Similar to the brands example in the blog post, you would use checkboxes with
name="eventDateTime[]"
instead ofname="eventDateTime"
:And you would use
$eventDateTime = $_GET['eventDateTime']
instead of$eventDateTime = perch_get('eventDateTime')
to get the values from the URL:The above gets you an array of the selected months.
Thanks alot, Hussein. I will look into it.
I now have this code:
My form:
Now I have 2 problems. The checkbox is not checked if an eventDateTime is in the query. Also if I filter by 2 different dates, for example september AND oktober it doesn't work.
A good approach here is to hard code the values in the filter and check you're getting the results you expect.
You are using
'match' => 'between',
and allowing the user to select more than two dates. If you're usingbetween
, thenvalue
can only have 2 values (a max and a min) as far as I know: