Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
jmd_count: Count MySQL tables
Pretty simple plugin – count MySQL table rows and optionally evaluate them in a conditional.
(Split from jmd_dashboard).
Last edited by jm (2008-05-23 16:08:24)
Offline
#2 2008-05-23 11:46:03
- Ruhh
- Member

- From: dakota dunes
- Registered: 2008-01-20
- Posts: 305
Re: jmd_count: Count MySQL tables
Neato.
I used the first function to count articles, comments, etc. :)
<txp:Ruhh />
Offline
Re: jmd_count: Count MySQL tables
Yeah, nice one jm. Very useful, thanks.
Out of curiosity, was there a reason you wrote your own SQL statement in the code instead of using safe_count()?
Last edited by Bloke (2008-05-23 12:22:22)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#4 2008-05-23 12:20:32
- uli
- Moderator

- From: Cologne
- Registered: 2006-08-15
- Posts: 4,316
Re: jmd_count: Count MySQL tables
Wow, jm! Didn’t expect it so quickly! Good to have it!
In the help’s example you use it for statistics. I could imagine it as something more valuable: For a small business, if stocks go below n, automatically send «Go baking!» mail to the bakery at OnUnload. Or similar, if site comments begin to pile up, red warning lamps could flash on the dashboard/assistant slave is skyped up ;)
Thanks, jm!
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
#5 2008-05-23 12:27:01
- uli
- Moderator

- From: Cologne
- Registered: 2006-08-15
- Posts: 4,316
Re: jmd_count: Count MySQL tables
Ah, sorry missed the “ridonkulous” example.
Would you please be so kind and explain a little more on
<txp:jmd_count_value/> Returns the value from jmd_if_count.
Where is it necessary/different from jmd_count?
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: jmd_count: Count MySQL tables
uli wrote:
Where is it necessary/different from jmd_count?
When you use jmd_if_count it sets a global variable on the page with the return value of the specified count. jmd_count_value simply retrieves that value of the “last if” in case you need it later on the page as well as the conditional behaviour.
Last edited by Bloke (2008-05-23 12:31:36)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#7 2008-05-23 13:39:47
- uli
- Moderator

- From: Cologne
- Registered: 2006-08-15
- Posts: 4,316
Re: jmd_count: Count MySQL tables
Thanks, Stef! Figured it out in the meantime, but can’t get if_count working.
I’m using exactly the same WHERE phrase as in a working instance of jmd_count:
<txp:jmd_if_count eval=">5" table="textpattern" where="image != ''">More than 5 images used in articles</txp:jmd_if_count /> (Rather useless stuff, but … well, for training ;) It doesn’t output the enclosed content, in no constellation of >5, <5, =5. It complains about the unexpected equal-sign/T_string (IS) or does nothing, whereas if_count seems to give me the total amount of articles. Am I missing something from the plugin help again?
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: jmd_count: Count MySQL tables
Uli, that cannot be parsed correctly by the tag parser in TXP 4.0.6 and earlier. (will work in 4.0.7)
The > before the 5 will close the tag prematurely.
Also, this: </txp:jmd_if_count /> should be </txp:jmd_if_count>
Offline
#9 2008-05-23 14:02:33
- uli
- Moderator

- From: Cologne
- Registered: 2006-08-15
- Posts: 4,316
Re: jmd_count: Count MySQL tables
</txp:jmd_if_count />
ooOOPPS! =)
Edit: I developed the minimal parser: Removing the second slash shows all the fine errors you’d expect it would :))
Last edited by uli (2008-05-23 14:05:35)
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: jmd_count: Count MySQL tables
Bloke wrote:
Out of curiosity, was there a reason you wrote your own SQL statement in the code instead of using safe_count()?
Just in case users want to count a whole table without a where clause – it’s unfortunate that safe_count and safe_rows have where hardcoded :(.
uli wrote:
<txp:jmd_if_count eval=">5" table="textpattern" where="image != ''">More than 5 images used in articles</txp:jmd_if_count />
Make sure those columns actually exist (hint: execute describe textpattern from a MySQL client).
ruud wrote:
Uli, that cannot be parsed correctly by the tag parser in TXP 4.0.6 and earlier. (will work in 4.0.7)
The > before the 5 will close the tag prematurely.
It’s all your fault Ruud! I just love the new tag parser :).
I’ve updated the plugin to 0.1.1 with 4.0.6 compatibility. Uli, use eval=">5".
Offline
#11 2008-05-23 16:30:47
- uli
- Moderator

- From: Cologne
- Registered: 2006-08-15
- Posts: 4,316
Re: jmd_count: Count MySQL tables
jm wrote:
Make sure those columns actually exist (hint: execute
describe textpatternfrom a MySQL client).
I didn’t employ smd_query previously ;) So “image” exists in “textpattern”, yes.
I’ve updated the plugin to 0.1.1 with 4.0.6 compatibility. Uli, use
eval=">5".
Sorry for all that hassle, but I’m really grateful that you did :)
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: jmd_count: Count MySQL tables
uli wrote:
So “image” exists in “textpattern”, yes.
Ah right, I forgot article_image is just called Image. Self: read your own hint!
Sorry for all that hassle, but I’m really grateful that you did :)
It was only one line, so it’s not a big deal. Enjoy!
Offline
Re: jmd_count: Count MySQL tables
Is it possible with jmd_if_count to use more than one parameter in the where attribute?
I need to test the number of articles returned in a yearly archive page, so I need to limit by both section and date.
My unsuccessful suck it and see test was this:
<txp:jmd_if_count table="textpattern" eval=">1" where="section='news' AND year='2009'">there's more than 1</txp:jmd_if_count>
But in reality I’d need to pull the year value from a txp:variable too, so that might complicate the syntax even further – something like:
<txp:jmd_if_count table="textpattern" eval=">1" where="section='news' AND year='<txp:variable name="this_year" />'">there's more than 1</txp:jmd_if_count>
cheers
Stu
Offline
Re: jmd_count: Count MySQL tables
pieman wrote:
My unsuccessful suck it and see test was this:
The plugin uses SQL. There isn’t a field called date. Also when you use = sign, it matches the value as is. No posted date is just exactly “2009”, but unix datetime.
Posted rlike '<txp:variable name="this_year" />' and section="news"
Offline
Re: jmd_count: Count MySQL tables
Gocom wrote:
The plugin uses SQL. There isn’t a field called
date.
Duh. Of course, sorry. I’ve successfully crammed the year into the month attribute of article_custom before and was clumsily trying to achieve the same thing.
Posted rlike '<txp:variable name="this_year" />' and section="news"
Thanks for your help but I’m not sure how that could work in this context. Do you mean to use it inside the where attribute?
<txp:jmd_if_count table="textpattern" eval=">2" where="Posted rlike '<txp:variable name="this_year" />' and section='news'">more than 1 - </txp:jmd_if_count>
I suspect I still have the wrong end of the stick, because that throws errors.
Offline