Jump to content

Selector for similar titles


Matzn
 Share

Recommended Posts

I have some page titles, eg.:

  • green tree 1,5 m
  • brown tree 1,5 m
  • green tree 11,5 m
  • brown tree 11,5 m
  • yellow banana 0,5 m

To find all 1,5 m trees is my selector ('title%=tree, title%="1,5 m"') , but results are also 11,5 m trees.

My first idea was to exclude "11,5 m"  ('title%=tree, title%="1,5 m", title!%="11,5 m"')

But exclude 1,5 m for 11,5 m searching i get not results ('title%=tree, title%="11,5 m", title!%="1,5 m"')

Yes, i have tested *=, ~=.

What am i missing?

 

Link to comment
Share on other sites

This is tricky because for the database "1,5m" is neither a word nor a number, so the comma is treated as a word boundary. This is why can't use selectors that make use of fulltext indexes (*=, ~=). The only option in this case is a slower LIKE comparison (%=), but that isn't aware of word boundaries, resulting in your problem with matching "11,5m" as well as "1,5m".

A quick and dirty solution would be to include the space in your selector, but I'm not even sure if ProcessWire won't filter out any leading/trailing spaces in selector values:
'title%=" 1,5 m"

The best solution is to turn the height of the tree into its own field which can be searched and compared properly. For the height, a simple integer field containing the height in centimeters would be appropriate. Then you can easily search for trees of a certain height, or even within a certain range of heights:

// all trees that are exactly 1,5m high
$trees = $pages->find('template=tree, height_cm=150');

// all trees that are between 1 and 2 meters high
$trees => $pages->find('template=tree, height_cm>100, height_cm<200');

 

  • Like 3
Link to comment
Share on other sites

And if you want the height to appear after the title in the page tree, you can use the "List of fields to display in the admin Page List" setting on the Advanced tab of the template's settings. And if you want to go even further and convert cms to decimal meters, you can use a hook something like this:

$this->wire()->addHookAfter('ProcessPageListRender::getPageLabel', function($event) {
    $p = $event->arguments[0];
    if($p->template == 'tree') {
        $event->return = $p->title . ' ' . ($p->height_cm / 100) . " m";
    }
});

 

  • Like 2
Link to comment
Share on other sites

A dedicated field for height is definitely the way to go, but just wanted to add that for the example titles you gave, the height follows the noun and if that applied consistently then potentially you could include the noun in the search text:

2021-01-06_095438.png.01d856ae2974e68ccf8c4d8e087a747a.png

 

  • Like 2
Link to comment
Share on other sites

Thanks all for yous ideas. I must had a tittle more explain my project. The page listing all trees und i use the height for a ajax filter function. I wan´t add a new field. It was more about the basic to the selectors.

Hope external links ok. A example: https://gutschein-für.de/preisvergleich/layher-uni-komfort-fahrgeruest-universalgeruest-mit-treppenaufstieg/

@MoritzLost

It would be good, but you dirt solution don´t work 'title%=" 1,5 m". All Whitspaces lost.

@Robin S

Yes, if add a additional word befor it works, but need only "1,5m"/"11,5 m"

Link to comment
Share on other sites

Hui, the solution is very easy:

$matches = $pages->find("selector")->not("selector");

Example for trees:

//all 1,5m no 11,5m
$matches = $pages->find('title%=tree, title%="1,5 m")->not(title%="11,5 m"');

//all 11,5m no 1,5m
$matches = $pages->find('title%=tree, title%="11,5 m")->not(title%="1,5 m"');

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...