Jump to content

Selector pageArray using AND not OR


arpeekay
 Share

Recommended Posts

$features = $pages->get('/restaurant/features/')->children;

foreach($features as $feature){	
	if(!$input->post[$feature->name]){$features->remove($feature);}
}

if(count($features) > 0){
	$restaurants = $pages->find("restaurant_features=$features")
}

This code returns the pages where one condition is met.

I need it to only return the pages where ALL conditions are met.

The query ends up look like so:

"template=restaurant_features=1052|1053|1054|1115"

Not sure where to go from here, any help would be greatly appreciated. 

Link to comment
Share on other sites

Hi, 

Welcome to the forum...

May be something like this would help you:

<?php
//Fetch results from restaurant/features where page id mixed
$parentPages = wire('pages')->find("parent=/restaurant/features, id=1052|1053|1054|1115, sort=sort");
?>
<ul>
   <?php foreach($parentPages as $childPage): ?>
   <li>
     <a href="<?php echo $childPage->url?>">
       <?php echo $childPage->title; ?>
     </a>
   </li>
   <?php endforeach; ?>
</ul>
Link to comment
Share on other sites

Got it working. Instead of using an array I just had to check each feature in its own query

"template=restaurant_features=1052|1053|1054|1115"

will return restaurants with any of those features

vs

"template=restaurant_features=1052, restaurant_features=1053, restaurant_features=1054, restaurant_features=1115"

will return restaurants with ALL of those features

Link to comment
Share on other sites

Hi arpeekay,

Welcome to ProcessWire and the forums...

Yes, you are right. PW uses "," [i.e. a comma] as AND. The pipe symbol | is used just like in normal PHP "OR".

http://processwire.com/api/selectors/#or-selectors1 - OR

http://processwire.com/api/selectors/#and-selectors - AND

But maybe there is a prettier way of writing those long AND conditions? - just wondering....

Edited by kongondo
Link to comment
Share on other sites

"template=restaurant_features=1052|1053|1054|1115"

This isn't a valid selector. I'm guessing just a typo and you mean something like this?

template=restaurant, features=1052|1053|1054|1115

Quoting Pete: 

if(!$input->post->{$feature->name}) 

You can also do this:

if(!$input->post($feature->name)) 
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...