bwakad Posted April 20, 2014 Share Posted April 20, 2014 I have this code which is for filtering/searching items: if (count($input->get)) { // Get the criteria and sanitize them $branche = $sanitizer->selectorValue($input->get->branche); $contract = $sanitizer->selectorValue($input->get->contract); // Search pages $selects = $pages->find("template=child-template, branche=$branche,contract=$contract"); } The problem is, it represents AND as in branche AND contract. In the API I see a pipe symbol is for OR, but this obviously is not working: $selects = $pages->find("template=child-template, branche=$branche|contract=$contract"); I need it to be some, all, or no fields. Is that possible? the search form has code like this: <div> <label for='search_branche'>Branche <select id='search_branche' name='branche'> <option value=''>Any</option> <?php // generate the Branche options, checking the whitelist to see if any are already selected foreach($pages->get("/branche/")->children() as $branche) { $selected = $branche->name == $input->whitelist->branche ? " selected='selected' " : ''; echo "<option$selected value='{$branche->name}'>{$branche->title}</option>"; } ?> </select> </label> </div> Link to comment Share on other sites More sharing options...
Macrura Posted April 20, 2014 Share Posted April 20, 2014 well you're not making it clear what you want your selector to be and you are probably making it too complicated. you should check the way the search processor i posted is setup, in that code each discreet element in the selector is added after checking to see if there exists the get variable. Link to comment Share on other sites More sharing options...
Soma Posted April 21, 2014 Share Posted April 21, 2014 There's no OR for multiple field=value selectors. Only for fields and or values. Like field1|field2|field3=value or field1|field2|field3=value1|value2 So maybe this can work for you. $selects = $pages->find("template=child-template, branche|contract=$branche|$contract"); 3 Link to comment Share on other sites More sharing options...
Pete Posted April 21, 2014 Share Posted April 21, 2014 I like Soma's example above as it contains a few things I didn't even know were possible (and I've been here 3+ years ). The only other way that might work for you is merging two PageArrays, like this (from this topic: https://processwire.com/talk/topic/3384-search-with-merged-pagearrays-pagination-not-working/): $selects = new PageArray(); $selects->import( $pages->find( "your first selector" ) ); $selects->import( $pages->find( "your second selector" ) ); $selects now contains pages from both selectors. Link to comment Share on other sites More sharing options...
bwakad Posted April 21, 2014 Author Share Posted April 21, 2014 Hi, The solution posted earlier by Macrura was a bit to complicated for my needs, to much fields and variables, did not see instructions. It might have worked though. I have tried the suggested solutions given by soma and pete, but the second field was either giving me a error 500 or nothing at all. I then tried it like this and it's working when I merge the fields into 1 variable $selector - have to do some more tests though - : --- SEARCH.PHP ---- $selector = ''; if($input->get->branche) { // Get the criteria and sanitize them $branche = $pages->get("/branche/" . $sanitizer->pageName($input->get->branche)); if($branche->id) { $selector .= "branche=$branche, ";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('branche', $branche->name); } } if($input->get->contract) { // Get the criteria and sanitize them $contract = $pages->get("/contract/" . $sanitizer->pageName($input->get->contract)); if($contract->id) { $selector .= "contract=$contract, ";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('contract', $contract->name); } } // Search pages $selects = new PageArray(); $selects->import( $pages->find( "template=child-template, $selector" ) ); Continued --------------- Now, since i could not catch the current page in my selector easy, I thought, let me turn it around and just provide the value from this current page. So I came up with this solution: ------ SEARCH-FORM.PHP ------- if ($page != $page->rootParent){ $value = $page->rootParent->name; $parents = array("provincie", "contract"); if (in_array($value, $parents)) { $value = $page->name; $option = ''; } } else { $value = ''; $option = 'Any'; } So for example, if my current page rootParent is 'provincie', it will take the child page name as value. And inside my form at <option> I use this: <option value='<?php echo $value;?>'><?php echo $value;?><?php echo $option;?></option> The only problem is, the value get filled in on all <option>'s of the form.... Link to comment Share on other sites More sharing options...
Pete Posted April 21, 2014 Share Posted April 21, 2014 Well a 500 error simply means there was something broken in your code. Returning "nothing at all" (I'll assume it returned no results is what you mean) means you were closer to getting it right but the selector probably needed tweaking. You can change the last bit of your code above to simply be: // Search pages $selects = $pages->find( "template=child-template, $selector" ) ); There's no need to import into an empty PageArray unless you were merging multiple PageArrays, so just doing the above results ina single PAgeArray anyway. Your code looks like it should be fine but will always result in a trailing comma in your selector (both the $selector.= lines end in a comma) so you should put the comma before the branche= and contract= lines and change your final selector to look like this (no comma): // Search pages $selects = $pages->find( "template=child-template $selector" ) ); Whole thing then looks like this: $selector = ''; if($input->get->branche) { // Get the criteria and sanitize them $branche = $pages->get("/branche/" . $sanitizer->pageName($input->get->branche)); if($branche->id) { $selector .= ", branche=$branche";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('branche', $branche->name); } } if($input->get->contract) { // Get the criteria and sanitize them $contract = $pages->get("/contract/" . $sanitizer->pageName($input->get->contract)); if($contract->id) { $selector .= ", contract=$contract";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('contract', $contract->name); } } // Search pages $selects = $pages->find( "template=child-template $selector" ) ); 2 Link to comment Share on other sites More sharing options...
bwakad Posted April 21, 2014 Author Share Posted April 21, 2014 Thanks Pete, did what you said (except the last part): $selects = $pages->find( "template=child-template, $selector" ) ); I had to use the comma there because initially the selector is empty : $selector = ''; For my search-form.php I also had to change my code - to capture the name, rootParent was actually not good - so I use this code to send to the search.php (although I had in mind to avoid all the if{} statements for every <option> I do not see another way: <?php $value = $page->parent->get("name") ; if ($value === "branche") { ?> <option value='<?php echo $page->name;?>'><?php echo $page->name;?></option> <?php } else { ?> <option value=''>Any</option> <?php } ?> But then of course, this only works once. I can't use this code twice because after sending the form, my $page has changed to search.php and $page->parent is website root. Link to comment Share on other sites More sharing options...
Pete Posted April 21, 2014 Share Posted April 21, 2014 Thanks Pete, did what you said (except the last part): $selects = $pages->find( "template=child-template, $selector" ) ); I had to use the comma there because initially the selector is empty : $selector = ''; Erm... that doesn't make sense. If the $selector variable is empty (as in neither branche or contract are being searched for), it translates to this when the variables are parsed as text (note the blank space after the comma as $selector contains nothing): $selects = $pages->find( "template=child-template, " ) ); My way would work and you wouldn't end up with a trailing comma. Link to comment Share on other sites More sharing options...
bwakad Posted April 21, 2014 Author Share Posted April 21, 2014 This works: $selects = $pages->find( "template=child-template, $selector" ) ; This is blanco: $selects = $pages->find( "template=child-template $selector" ) ; Maybe because initially you would need a comma between the first part (template) en second part (field). Strange thing is.... When I choose a value on search form: In the first which works - I would think I have two comma's - but get results. Second part which does not work - make me believe I have a single comma - but no results. ps. when no value is chosen, the $selector is not applied because: if($input->get->branche) { Link to comment Share on other sites More sharing options...
Pete Posted April 21, 2014 Share Posted April 21, 2014 $selector .= ", branche=$branche"; Note I moved the comma before the branche= and contract= when I did my examples a few posts up Link to comment Share on other sites More sharing options...
bwakad Posted April 21, 2014 Author Share Posted April 21, 2014 Yes, I did understand that. Here is the code I changed earlier to what you said: if($input->get->branche) { // Get the criteria and sanitize them $branche = $pages->get("/branche/" . $sanitizer->pageName($input->get->branche)); if($branche->id) { $selector .= ", branche=$branche";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('branche', $branche->name); } } if($input->get->contract) { // Get the criteria and sanitize them $contract = $pages->get("/contract/" . $sanitizer->pageName($input->get->contract)); if($contract->id) { $selector .= ", contract=$contract";//CHANGE YOUR SELECTOR AS SHOWN HERE $input->whitelist('contract', $contract->name); } } // Search pages $selects = $pages->find( "template=child-template, $selector" ) ; foreach ($selects as $child) { echo $selector; // added this to see the value given Then, I changed the last line (removed the comma before $selector) but that gave zero results, and no selector value. Putting it back: Leaving the code (with comma) and NOT choosing anything on the search form - clicking search, I get back all items and no selector value. Leaving the code (with comma), branche and contract on the search form - clicking search, I get back all items asked, and a selector value ", branche=1027, contract=1145". One addition: my url looks like this: /host/search/?provincie=&branche=accountancy&contract= So could that be the issue? Link to comment Share on other sites More sharing options...
Roope Posted April 21, 2014 Share Posted April 21, 2014 Shouldn't that find selector be like this? $selects = $pages->find( "template=child-template" . $selector ) ; Link to comment Share on other sites More sharing options...
bwakad Posted April 21, 2014 Author Share Posted April 21, 2014 Well Roope, That was probably the solution to the mysterious comma issue! I now have all results when I do not choose a value, and when I do choose a value I get the desired results - for instance a selector value ", branche=1027, contract=1145". So, now I need to continue to capture the chosen values back into my search form while the current page is changed to search.php where my results are processed. The form is in my head.inc, but the search.php is just a page being send to. On a normal page I could go for if ($page->parent...etc. on the search.php this is not possible. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now