Jump to content

Fetching and filtering field values (fetch all tags)


Rob
 Share

Recommended Posts

I've created a page that is basically used as a big image repository. The page has a single field with multiple items. Each item is an image field with extra parts to add tags.

So what I want to do is pull a list of every image from that field in that page (no problem) but then pull every unique value from the "tags" part of the field within each image.

So if I wanted to create a tag cloud based on the values within a certain field, how might I do that?

To clarify - I can grab a single page and then every image in a multi-item field, but what I then need is the value from the 'tags' part of each of those images.

Link to comment
Share on other sites

Where as the tags stored? As part of the 'description' field for each image, or are you using a custom Fieldtype/Inputfield for your images? I'm going to assume they are in your description field, so you could do something like this:

$tags = array();
$tagCloud = array();

foreach($page->images as $image) {
   $t = explode(' ', $image->description);
   $tags = array_merge($tags, $t);  
}

foreach($tags as $tag) {
   if(isset($tagCloud[$tag])) $tagCloud[$tag]++;
       else $tagCloud[$tag] = 1;
}

ksort($tagCloud);

The end result would be an alphabetically sorted $tagCloud array with each key being the tag and each value being the number of times the tag appeared. So you could output it in some manner like this:

foreach($tagCloud as $tag => $quantity) {
$fontSize = 10 + $quantity;
echo "<span style='font-size: {$fontSize}px;'>$tag</span> ";
}
  • Like 1
Link to comment
Share on other sites

Thanks for the code exampel Ryan, that's basically the kind of thing I was already doing, I was just hoping there would be a simple way to do it with API, selectors etc. Seems like a lot of DB load to fetch all that stuff just to get a small part of it.

What I've actually done is just so a direct DB fetch for everything from the tags table, as I was using a custom Fieldtype.

I don't actually mind using direct DB access where it makes sense, although I was a bit confused by your DatabaseQuery and DatabaseQuerySelect classes (I think they were called) as I couldn't work out how to use them and how to add criteria then make a DB query call.

Not to worry!

Link to comment
Share on other sites

Those DatabaseQuery classes are primarily so that the Fieldtypes can talk to PageFinder to construct an SQL query more abstractly. But for any situation where queries aren't being passed around to different classes, I prefer to write the query rather than abstracting it. PW isn't trying to introduce a new database layer. The database class is just PHP's mysqli with some debug logging options. For the most part I love the tools that PHP provides and don't like too many extra layers.

Btw, the code sample I posted should only result in 1 query (assuming 1 page). When you say lots of queries, that makes me think you are constructing a tag cloud from lots of pages (?). You can always reduce queries by using the 'autojoin' option with a field, or by using a FieldtypeCache. But if you know how to write efficient database queries, then that will always be the fastest.

Though also want to mention that it's futile to measure anything by quantity of queries in MySQL... it will execute a few hundred simple/optimized queries a lot faster than just 1 complex/unoptimized query. I used to try really hard to keep queries to an absolute minimum. But have since learned that from a performance standpoint, there are many situations where more queries will make things go faster, not slower. So measuring things by time is a lot more useful than measuring by query quantity.

Link to comment
Share on other sites

  • 4 years later...

I was using Ryan's suggestion (#2) and it worked quite well until I added a new tag to my list. This one was added to the alphabetically sorted list at the end, inspite it was a 'a...' word- which should be on top of the list. It looks that the [key] of my $tagCloud array is using the tag ID, and when sorted they will just result in an sequential row of the IDs.Originally I had imported all Tags, ordered alphabetically- so the issue was not obvious up front.

How can I use the $tag->title for sorting?

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