Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2013-08-20 14:33:11
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
count number of article images
There’s probably a really simple answer to this, but right now I’m stumped…
I’m looking for a way to count the number of article images associated with the current article.
My articles generally have multiple article images (comma separated list in the article image field). On a page displaying the individual article, I need the number of article images. So when the article image field reads “1,2,3” (ie, the article images for this article are those with ID 1, ID 2 and ID 3), I need a tag that puts out “3” because there are three images associated with this article.
So far I’ve found ckr_image_count which can count the total number of images that were uploaded to textpattern or the number of images assigned to a specific category, and I’ve found glx_image_count which seems to look at the body of an article and counts the images in there.
I feel I’m missing something really obvious here, so I really hope someone can point me in the right direction! Many thanks in advance
Offline
Re: count number of article images
You could get the count by counting the number of value in the field. This could be done using rah_function plugin:
<txp:if_article_image>
<txp:rah_function call="substr_count" haystack='<txp:custom_field name="article_image" />,' needle="," />
</txp:if_article_image>
Or in raw PHP:
<txp:if_article_image>
<txp:php>
echo substr_count(parse('<txp:custom_field name="article_image" />,'), ',');
</txp:php>
</txp:if_article_image>
Both basically count the number of commas in the field (and add one) using PHP’s substr_count function.
Last edited by Gocom (2013-08-20 14:54:30)
Offline
#3 2013-08-20 15:01:39
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
Re: count number of article images
Gocom wrote:
You could get the count by counting the number of value in the field.
Alright, I’ll just use the raw PHP then. I need to do some math with that number anyway. Thanks for your comprehensive and super fast answer!
Offline
#4 2013-08-20 15:17:08
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
Re: count number of article images
Gocom wrote:
Both basically count the number of commas in the field
Additional little problem: I know I said my article_image fields were comma separated lists… well, I forgot that sometimes they also specify a range. Ie, “1-3” instead of “1,2,3”. Or even a combination of comma separated values and ranges (“1-3,5”)
Any ideas on how to deal with that?
Offline
Re: count number of article images
M_i wrote:
I forgot that sometimes they also specify a range
Remember that Textpattern itself doesn’t support that. If you want to use Textpattern core tags such as images, that won’t work. Possibilities are that is going to break in the future if the storage method for images ever changes.
But if you want to support an custom pattern, then you would have to apply the same logic as you are using to read the values from the field. E.g.
<txp:php>
$images = parse('<txp:custom_field name="article_image" />');
$count = 0;
foreach (do_list($images) as $value)
{
if (strpos($value, '-'))
{
$value = do_list($value, '-');
$count = count(range((int) $value[0], (int) $value[1])) + $count;
}
else
{
$count++;
}
}
echo $count;
</txp:php>
Last edited by Gocom (2013-08-20 15:32:14)
Offline
#6 2013-08-20 15:49:33
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
Re: count number of article images
Gocom wrote:
Remember that Textpattern itself doesn’t support that.
Yeah, I know. But the excellent smd_slimbox does…
<txp:php>
$images = parse('<txp:custom_field name="article_image" />');
$count = 0;
foreach (do_list($images) as $value)
{
if (strpos($value, '-'))
{
$value = do_list($value, '-');
$count = count(range((int) $value[0], (int) $value[1])) + $count;
}
else
{
$count++;
}
}
echo $count;
</txp:php>
Thanks! You’re a star!
Offline
#7 2013-08-21 09:32:32
- Gallex
- Member
- Registered: 2006-10-08
- Posts: 1,289
Re: count number of article images
and if i would like to count images under image category named ‘gallery’?
<txp:php>
echo substr_count(parse('<txp:category="gallery" type="image" />,'), '.jpg');
</txp:php>
?
Offline
#8 2013-08-21 09:57:22
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
Re: count number of article images
Gallex wrote:
and if i would like to count images under image category named ‘gallery’?
I think for that you could use ckr_image_count or glx_image_count
Offline
#9 2013-08-21 10:01:08
- M_i
- Member
- Registered: 2006-03-05
- Posts: 122
Re: count number of article images
Gocom wrote:
But if you want to support an custom pattern, then you would have to apply the same logic as you are using to read the values from the field.
Ah, but that doesn’t work for more complex patterns. For example, I have one article with a article_image field that reads “49, 47-48, 50-52, 55, 57”. (It’s like that because the first image ID of the field (here 49) denotes the image used in lists.)
Offline
Re: count number of article images
Gallex wrote:
and if i would like to count images under image category named ‘gallery’?
<txp:php>echo safe_count("txp_image", "category='gallery'");</txp:php>
Offline
Re: count number of article images
M_i wrote:
Ah, but that doesn’t work for more complex patterns. For example, I have one article with a article_image field that reads “49, 47-48, 50-52, 55, 57”.
I don’t see why that wouldn’t work, but it’s error-prone (imagine a field like "48, 47-49, 49"
, or an id
that does not exist). A more robust solution is
<txp:php>
$images = parse('<txp:custom_field name="article_image" />');
$ids = array();
foreach (do_list($images) as $value)
{
if (strpos($value, '-'))
{
$value = do_list($value, '-');
$ids = array_merge($ids, range((int) $value[0], (int) $value[1]));
}
else
{
$ids[] = (int) $value;
}
}
if($ids) echo safe_count("txp_image", "id IN ('".implode("','", $ids)."')");
else echo 0;
</txp:php>
Last edited by etc (2013-08-21 20:19:46)
Offline
Re: count number of article images
etc wrote:
A more robust solution is
I would discourage calling tag functions directly. Its way more likely that the function will be gone, than the parser or the syntax changes. As long as PHP doesn’t reserve parser as function or keyword.
In 4.6.0 we no longer call functions by the tag name itself; instead it uses a registry and instances. Likelihood is that eventually these functions and their global variables will be gone. In 4.6.0 all new tags and yield, due to it being keyword in PHP 5.5, will not have matching functions. The only documented way of accessing the tags is by template strings, or querying the registry. The current development registry is subject to some changes; mainly a container and the statics will be gone with it.
"id IN ('".implode("','", $ids)."')");
If you want, you can drop the single quotes. The array only contains integers and even if it didn’t, the quotes alone only mask the SQL injection vulnerabilities.
Offline