Need min-width filter for pdoPage

Looking for some help / advice somehow add a minimum width filter to a page that uses pdoPage.

I already use Tagger with pdoPage together with HTMX and to have a nice AJAX filter.

But am lost in how to create a option where you can fill in 1200 width (in cm) and only display resources with a TV product_width of 1200 or above…

This needs to work in combination with the other (Tagger based) filters already active.
Code I use now for the filter:

<script>
  function filterProducts() {
    let str = "";
    const inputs = document.getElementsByClassName("filterProducts");
    for (const input of inputs) {
      if (input.checked) str += input.value + "&";
    }
    let div = document.getElementById("pdopage");
    div.setAttribute("hx-get", "product-list.html?" + str);
    htmx.process(div);
    htmx.trigger("#pdopage", "search");
  }
</script>

The snippet TaggerGetResourcesWhere has a property &where where you can pass in where conditions (that you would normally use directly with pdoResources/pdoPage).

Or maybe you could also use the &tvFilters property.

Thx of course, that makes sense.
I already have:

&where=`[[!TaggerGetResourcesWhere? &matchAll="1"]]`

Any idea how I would adjust

&where=`{"stone_width": 1200}`

To be stone_width equals 1200 or above?

Theoretically it’s &where=`{"stone_width:>=": 1200}` , but it seems that pdoResources makes a string-comparison (that will returns the wrong results).

I believe “tvFilters” (&tvFilters=`stone_width>=1200` ) should work correctly.

Or maybe use a string like this:

&where=`["CAST(stone_width AS SIGNED INTEGER) >= 1200"]`

Thanks!

&tvFilters=`stone_length>=600`

Works fine, now I just need to figure out how to trigger this using a input field above the table…

You could do something like this:

&tvFilters=`[[!getWidthFilter]]`

Snippet getWidthFilter:

<?php
if (isset($_GET['minwidth'])) {
    $width = intval($_GET['minwidth']);
    return "stone_length>=" . $width;
}
return '';

And then call the page with a request paramater → mypage.html?minwidth=600

1 Like

You are a legend sir.

It is not working when I combine two filters…

Is my PHP wrong? (not very strong in PHP)

<?php
if (isset($_GET['minwidth'])) {
    $width = intval($_GET['minwidth']);
    return "stone_width>=" . $width;
}
if (isset($_GET['minlength'])) {
    $length = intval($_GET['minlength']);
    return "stone_length>=" . $length;
}
return '';

pdoPage call:

&tvFilters=`[[!getFilter]]`

You have to combine the filter-strings.
For example like this:

$filters = [];
if (isset($_GET['minwidth'])) {
    $width = intval($_GET['minwidth']);
    $filters[] = "stone_width>=" . $width;
}
if (isset($_GET['minlength'])) {
    $length = intval($_GET['minlength']);
    $filters[] = "stone_length>=" . $length;
}
return implode(',', $filters); // using AND

The , character is to combine the filters with AND.
Use || instead, if you want to use OR.
(Take a look at the syntax here in the documentation.)

1 Like

You can also use FilterWhere which creates the query and fills placeholders with escaped values.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.