bwakad Posted March 30, 2014 Share Posted March 30, 2014 I have this code which I need to rewrite to retrieve a list of children pages from the parent under location. location - parent01 - - child - - child - parent02 - - child - - child foreach($pages->get("/location/")->children() as $municipal) { I need to collect the childs. Is there something for this? Because I tried siblings() but that did no good. Or do I need to first make a foreach on the parent, and inside foreach for the children? - if so is that good practise? Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Depends what you want to do. Seems not like it's not something really used or needed, collecting id's of children's children for whatever reason that is. Since we have an hierarchical structure and the API children parent, siblings is build for traversing, it may won't scale well (speaking of possibly thousands of pages) iterating with foreach and "collecting" ids. Always it's like this: for having a couple pages it may ok but won't scale infinitely. So it all depends largely on the structure you have and what templates they have. Taking all this into consideration will lead to new possibilities with API. Usually of how I would have this is that the child have their own template so you could use a $locationParent->find("template=subchild"), which would scale more but still maybe an issue if you go for very large sets of pages. But if it's only for small set of pages it may ok to just foreach all children and foreach each children of the children. So having to "collect id's" always rings a red alarm: "For what, and how many?". Link to comment Share on other sites More sharing options...
bwakad Posted March 30, 2014 Author Share Posted March 30, 2014 In total there are 400 pages as childrens. I use this as list select on a search form to search by municipal. Meanwhile I was looking further in it and also noticed I can use the following code, but it will only give me the list of children under the first parent. foreach($pages->get("children=/location/")->children() as $municipal) { Perhaps if it is to heavy - which I think it is - the municipal is a field, so I better use this next code, change it a little to search in fields. That way it possibly does not need to go through all entries: // if there are keywords, look in the title and body fields for the words if($input->get->keywords) { $value = $sanitizer->selectorValue($input->get->keywords); $selector .= "title|body%=$value, "; $summary["keywords"] = $sanitizer->entities($value); $input->whitelist('keywords', $value); } Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 You can't do children->children() cause children is a PageArray and not a single page. always page->children() Not sure what $pages->get("children=/location/")->children() is supposed to do? So you want to build a <select> dropdown on a front-end form with all 400 as options? 400 is already a lot but would be no problem foreach the children, even nested if needed. It's where it start to get slow and you notice it may take 1-2 seconds to generate, but the searching and loading with PW isn't the problem (at least with 400), it's rendering the list. So in this case rendering 400 options for a select it would be wise to output the resulting markup and caching it using MarkupCache. That allows you to cache certain parts only of a page for a certain time. That would speed up not "loading" the list everytime. After all you don't have a choice than to foreach all the pages at the end, no? Please always be more/very specific to what you need to achieve and we can help more focused instead of trying to find out first what you're doing before a valueable explanation can be done. That can be very frustrating and lead to nobody helping you anymore. Collecting id's is something different thatn building a select. I'll go focus on more important things now. Link to comment Share on other sites More sharing options...
bwakad Posted March 30, 2014 Author Share Posted March 30, 2014 Well, thanks for the effort anyway. ps. I thought I was clear on what I wanted to do - from the topic title and description in the first post - but maybe i'm thinking simple - and others think php Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 You're right, I'm just oversaturating a little. Post title doesn't say anything. Collecting children does a little more, but we do that all the time (I got two) Well there's sometimes a lot more than just how to, it's the what and where and why that may make a difference. 1 Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 So to respond to your first post again: I need to collect the childs. Is there something for this? Because I tried siblings() but that did no good. No there's not, but yes there is. Or do I need to first make a foreach on the parent, and inside foreach for the children? - if so is that good practise?" Yes. Yes. Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 And last some code when children would have their own template: $allChildren = $pages->get("/locations/")->find("template=child_tpl"); or even checking for parent template $allChildren = $pages->get("/locations/")->find("parent.template=parent_tpl"); or $allChild = new PageArray(); foreach($pages->get("/locations/")->children() as $parent) { foreach($parent->children() as $child) $allChildren->add($child); } and there's even more variations depending on who writes it. 3 Link to comment Share on other sites More sharing options...
bwakad Posted March 30, 2014 Author Share Posted March 30, 2014 ok, I did not blame you Thinking what you said about speed an all, I was using the below code and looked in the API selectors. In my search form: the search form has <p> <label for='search_keywords'>Keywords</label> <input type='text' name='keywords' id='search_keywords' value='<?php if($input->whitelist->keywords) echo $sanitizer->entities($input->whitelist->keywords); ?>' /> </p> then it's send to the search.php where this part give me an error: if($input->get->keywords) { $value = $sanitizer->selectorValue($input->get->keywords); $selector .= "title|body|municipal%=$value, "; $summary["keywords"] = $sanitizer->entities($value); $input->whitelist('keywords', $value);} I understood that it searches all fields for the value. If I use: $selector .= "title|body%=$value, "; everything fine, no error, but no search on municipal either. Link to comment Share on other sites More sharing options...
diogo Posted March 30, 2014 Share Posted March 30, 2014 Actually, this works: $pages->find("parent={$pages->get('/location/')->children()}"); 1 Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Actually, this works well: $pages->find("parent={$pages->get('/location/')->children()}"); That's also nice solution and something that usually works well for smaller amounts, just doesn't scale very good and high. It just takes a lot more power and memory in my tests back then with large sets of pages (+10'000), for larger than 10'000 I couldn't even execute it on without getting out of memory , while the a direct ("parent.template=parent_tpl") takes almost no time compared. 1 Link to comment Share on other sites More sharing options...
bwakad Posted March 30, 2014 Author Share Posted March 30, 2014 Well, I like to thank you all for thinking with me, but I have to say Soma was right. 400+ in a list on front end is not to good! But hey, at least I learned something... I will go for the sidebar and retrieve items from current page if any, so when I have a view of province, I get a list of cities. Link to comment Share on other sites More sharing options...
diogo Posted March 30, 2014 Share Posted March 30, 2014 but I have to say Soma was right. 400+ in a list on front end is not to good! You managed to confuse me with this. 400 in a list is not too much because you can always paginate it, so I had to look for the place where Soma had said that, and what he really says is that 400 in a dropdown is too much. Maybe I'm just being picky, but someone might read this and start avoiding having long list on their sites. Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Hm, I've never said 400 is too much, just that it will take some time to load and you could use markup cache Ryan does the same I guess for the modules.processwire.com dropdown with all 250 modules (actually maybe just ProCache anyway) Link to comment Share on other sites More sharing options...
bwakad Posted March 30, 2014 Author Share Posted March 30, 2014 Sorry for the confusion ... to tell you the truth, since I started this topic, and solutions where diverse, I really had to think which answer I had to marked as Solved. I was talking about a drop down list - 400+ should not be to much problem server side, but maybe it's also not very usefull for client side. Thinking client side, I prefer having a dropdown (if applicable) and when using a tab (keyboard) you see the whole list, so I can use my up/down keys or alphabet key to choose... but that's just me. That's why I had to go with soma answer. I'm learning from you all ! Link to comment Share on other sites More sharing options...
diogo Posted March 31, 2014 Share Posted March 31, 2014 Hm, I've never said 400 is too much Soma, that's what I'm saying. You never said it 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