joe_g Posted October 25, 2012 Share Posted October 25, 2012 Hey hello, Basic question: If I have tagged posts, and I would like to list all tags (in this case it's a bunch of associated pages; 'page'-type) that have been used at least once, I'd have to loop throught all posts, right? There is no shortcut? thanks! J Link to comment Share on other sites More sharing options...
ryan Posted October 26, 2012 Share Posted October 26, 2012 I think we might need more description. Are you referring to the blog profile? Can you tell more about the specific context of the question? Link to comment Share on other sites More sharing options...
joe_g Posted October 30, 2012 Author Share Posted October 30, 2012 Ah yes, I would need to explain a bit better perhaps. It's a general case where I have an association page A includes a bunch of page B's with a multiselect. I'd like to list all page B's that have been included one time or more into a page A. I guess (this is my question) the only way to find out what page B's that have been included somewhere is to loop through all page A's. I figure that multiselect, or any form of association is only one-way, not two ways? The B's are not aware of where they are included? thanks J Link to comment Share on other sites More sharing options...
diogo Posted October 30, 2012 Share Posted October 30, 2012 The B's are not aware where they are included, but they can ask: $pages_that_include_me = $pages->find("page_field=$page"); This will give you an array of all the pages that include the page you are in. Edit: Rereading your question I see that you where asking the opposite. Assuming that you want to list all B's that where included on any A page without repetitions, you can do this: foreach ($pages->find("template=B") as $b){ if($pages->find("template=A, page_field=$b")->count() > 0) echo $b->url; } Link to comment Share on other sites More sharing options...
joe_g Posted October 31, 2012 Author Share Posted October 31, 2012 Nice thanks, works great. I figure this would give a lot less sql queries than iterating over all template A's. I'm using it for Page (hundreds) and tags (a handful), and instead of looping through all hundreds of pages to check what tags are in use, I guess this would one sql select per tag. Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 13, 2012 Share Posted November 13, 2012 One additional question: Are there any performance drawbacks? Generating a list of ten or twenty category links must be quite CPU-intensive if Processwire has to iterate over hundreds of blog articles every time just to check if a category is used at least once. Link to comment Share on other sites More sharing options...
Adam Kiss Posted November 13, 2012 Share Posted November 13, 2012 @Michael: AFAIK PW takes that query and creates one SQL query for MySQL, so it isn't that CPU intensive - hundreds of rows are nothing for MySQL. But then again, there are ways to improve this, if it's too much: caching the results will give you first boost. Then, if it grows over time (and is killing the server), you always can go the 'half-static' way -having the numbers saved somewhere, and regenerate results based on hook to Pages::save(), etc. I think you get the point. 1 Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 13, 2012 Share Posted November 13, 2012 caching the results will give you first boost. That’s what I did in my current project. I have a “more topics in this blog“ box on every tag page. The box features a list of links to all tags – except the current tag page. So every tag page has a different "more topics" list, wich is cached for 24 hours. Probably a far longer cache period would be reasonable, since I’m not going to add new tags every day. <?php $tags = $pages->find("template=tag, sort=title"); $cache_topic_name = 'more_tags_list_for_' . $page->name; $cache = $modules->get("MarkupCache"); if(!$more_tags_list = $cache->get($cache_topic_name, 86400)) { $more_tags_list = '<ul>'; foreach ($tags as $tag) { if ($pages->find("template=blog-article, tags=$tag")->count() > 0) { $more_tags_list .= '<li><a href="' . $tag->url . '">' . $tag->title . '</a></li>'; } } $more_tags_list .= '</ul>'; $cache->save($more_tags_list); } echo $more_tags_list; ?> 1 Link to comment Share on other sites More sharing options...
ryan Posted November 14, 2012 Share Posted November 14, 2012 I'd suggest changing this line: if ($pages->find("template=blog-article, tags=$tag")->count() > 0) { To this: if ($pages->count("template=blog-article, tags=$tag") > 0) { The reason is that find() loads all those pages, whereas count() just counts them... far less overhead if you don't need the actual pages loaded (which it doesn't appear that you do). 2 Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 15, 2012 Share Posted November 15, 2012 The reason is that find() loads all those pages, whereas count() just counts them Great. I didn’t know that I can put a selector into “->count()”. Did I miss this in the API cheatsheet? Link to comment Share on other sites More sharing options...
ryan Posted November 15, 2012 Share Posted November 15, 2012 Great. I didn’t know that I can put a selector into “->count()”. Did I miss this in the API cheatsheet? Just checked -- it looks like its not in the cheatsheet. Soma, if you are reading this: can you add it, or tell me how to add it? 1 Link to comment Share on other sites More sharing options...
Soma Posted November 15, 2012 Share Posted November 15, 2012 If it's not in the sheet it's most likely also not in the documentation. I'll add it. I think there's still some useful things missing in documentation and so cheatsheet, but can't think of any ATM. 2 Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 15, 2012 Share Posted November 15, 2012 Since I can’t help so much with developing ProcessWire or PW modules (because I’m not a PHP expert yet), I’d be happy to help extending the documentation. So please tell me if I can help. 1 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