marcin Posted May 14, 2013 Share Posted May 14, 2013 Hello, I want to check whether PageArray contains some list of pages - as input I would like to use an array of pages' IDs. There is some elegant way to do it or I need to loop through PageArray and use $p->has("id=X")? Thanks, Marcin Link to comment Share on other sites More sharing options...
Wanze Posted May 14, 2013 Share Posted May 14, 2013 Hi marcin, I think there's nothing wrong with looping through the array and check with $p->has('id=X'). No more elegant way popping in my mind right now Link to comment Share on other sites More sharing options...
marcin Posted May 14, 2013 Author Share Posted May 14, 2013 Yes, it's true, but probably method provided by API would be more efficient. If there's nothing like this - nothing will happen. Link to comment Share on other sites More sharing options...
Soma Posted May 14, 2013 Share Posted May 14, 2013 Just $pageArray-> has("id=1001|1003") will do it. 4 Link to comment Share on other sites More sharing options...
Wanze Posted May 15, 2013 Share Posted May 15, 2013 @Soma nice. The problem may be if we want to match 10 IDs like this and let's say one of them is in the array, ->has would return true? Because '|' denotes an OR operator. So if you want to know exactly which ID is in there, the loop is necessary. Maybe I'm wrong Link to comment Share on other sites More sharing options...
Soma Posted May 15, 2013 Share Posted May 15, 2013 That's what I assume he wants... If you want to know which is in the page array use filter. $found = $pageArray->filter("id=1001|1003|1005"); echo $found; // 1001|1003 6 Link to comment Share on other sites More sharing options...
Wanze Posted May 15, 2013 Share Posted May 15, 2013 That's what I assume he wants... If you want to know which is in the page array use filter. $found = $pageArray->filter("id=1001|1003|1005"); echo $found; // 1001|1003 Awesome! How could I miss this in the Cheatsheet... again learned something new, thanks! Link to comment Share on other sites More sharing options...
marcin Posted May 15, 2013 Author Share Posted May 15, 2013 Unfortunately, I need to check whether all of them are in PageArray. But big thanks for help guys. Link to comment Share on other sites More sharing options...
Soma Posted May 15, 2013 Share Posted May 15, 2013 Well that doesn't really come through until now... So what now? If all of the "list" are in the PageArray (but also some others), or if the PageArray is the list 1:1... ? Now I'm curious why you want or need that? I would do that if you want to test if all of the list pages are in the PageArray: $result = $pages->find("template=basic-page"); $list = $pages->getById('1001,1002'); if(count($result->filter("id=$list")) == count($list)){ echo "all found"; } 2 Link to comment Share on other sites More sharing options...
kongondo Posted May 15, 2013 Share Posted May 15, 2013 $pages->getById This is new to me. I haven't seen it in the cheat sheet. Does it mean it doesn't grab the whole page object but only the IDs? Link to comment Share on other sites More sharing options...
Soma Posted May 15, 2013 Share Posted May 15, 2013 Yeah it's not (yet) on the Cheatsheet. No it does grab the pages by id as the name indicated ProcessWire never does grab the "whole" pages unless you need it or a field is autojoin. It's kinda lazy loading. 1 Link to comment Share on other sites More sharing options...
diogo Posted May 15, 2013 Share Posted May 15, 2013 same as $pages->find("id=1001|1002") or faster? Link to comment Share on other sites More sharing options...
marcin Posted May 15, 2013 Author Share Posted May 15, 2013 @Soma: I have attributes set as pages. So I have list of all attributes(list of pages under Attributes page) and Page inputfield in some template. Now, I want to search only these ones(from this template) which have selected all of attributes I choose. You idea seems to be quite nice thanks. Link to comment Share on other sites More sharing options...
marcin Posted June 9, 2013 Author Share Posted June 9, 2013 OK guys, problem is probably a bit more complicated - @Soma's solution won't work if I want to search for some list of pages' IDs in multiple page field(not through all pages in system). It's a part of quite complicated search, so list of items is processed a few time - using filter() is prefered, multiple loops would probably decrease performance. Example: I have list of pages: $itemsList - each item has attributes field(it's a page field). There is also array of attributes' IDs - I want to filter items which have all of attributes in attributes field. Thanks, Marcin Link to comment Share on other sites More sharing options...
ryan Posted June 15, 2013 Share Posted June 15, 2013 Since you are dealing with the contents of a Page reference field, already loaded in memory (it sounds like?), take a look at the WireArray section of the cheatsheet, which outlines several methods you can use. Be sure to show the "advanced" options in the cheatsheet, because there is a lot in there. Take a stab at it and post what you've got. From there, I think we'll have a better idea of what you are trying to do, as well as a solution, if you can't get it working. 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