Jump to content

Searching in select fields


Recommended Posts

Hi guys.

I'm trying to build a complex search and so far it seems over my head. Can't find an answer digging through the docs or the forum so here goes.

Basically I have a bunch of fields (textfields, textareas and selects) and I want the search keywords to be compared to values in all of them. I'm having trouble with the selects:

$selector = "ref|title|type|version|state%=$q";

Since 'state' is a select, I get...

Error: Exception: Operator '%=' is not implemented in FieldtypeSelect

Oh and since I'm here, there's another problem. I need this search to look for each word without regard for sequence. For example, if I have a page called "this and that" and search "this that" doesn't find it. I have tried using *tilde* instead of %, but that doesn't find keywords shorter than 4 chars and I need that as well.

Link to comment
Share on other sites

To circumvent the operator issue use or groups. 

$selector = "(ref|title|type|version%=$q), (state=$q)"

https://processwire.com/api/selectors/#or-groups
 
Edit:
 
To search for each word independent you need to define it that way:


$qs = explode(" ", $q);
$fields = explode("|", "ref|title|type|version|state");

foreach($fields as $f){
  $group = array();
  foreach($qs as $q){
    $group[] = "$f%=$q";
  }
  $selector .= ", (".implode(", ", $group).")";
}

resulting in

(ref%=this, ref%=that), (title%=this, title%=that), …
  • Like 5
Link to comment
Share on other sites

Hélder, before you choose FieldtypeOptions over pages (and may be the best option, depending on how simple is your task), make sure you understand the full potencial and flexibility that pages provide even when used for a simple dropdown select. You'll get it as soon as the weird factor goes away :)

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...