Jump to content

Recommended Posts

Posted

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

Posted

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?

Posted

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

Posted

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;
}
Posted

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.

  • 2 weeks later...
Posted

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.

Posted

@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.

  • Like 1
Posted

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;
?>
  • Like 1
Posted

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).

  • Like 2
Posted
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?

  • Like 1
Posted

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.

  • Like 2
Posted

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.

  • Like 1

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
×
×
  • Create New...