Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-01-12 11:41:00

raminrahimi
Member
From: India
Registered: 2013-03-19
Posts: 276

how to calculate custom_fields of articles

I’ve a category of Tasks,
each department has a specific article belong to tasks category, like finance department
each article has 3 custom_field (completed, ongoing, rejected)
I write the totals for each department to it’s custom fields, like finance department article has 20 completed, 30 ongoing, 5 rejected.
—-
Now I want to make a summary to my public report page:
1. to show by percentage of each department (article): how much % completed, % ongoing , % rejected
2. to show by percentage of all departments total (articles): how much % completed, % ongoing , % rejected
3. to show the total calculate of tasks (articles): how many completed, ongoing, rejected

Offline

#2 2019-01-12 15:26:32

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: how to calculate custom_fields of articles

Untested, but that looks doable in 4.7:

<txp:article_custom category="tasks">
<txp:variable name="total" escape="integer">
<txp:evaluate query='<txp:custom_field name="completed" /> + <txp:custom_field name="ongoing" /> + <txp:custom_field name="rejected" />' />
</txp:variable>
<h3><txp:title /></h3>
<p>
Completed: <txp:evaluate query='<txp:custom_field name="completed" /> * 100 div <txp:variable name="total" />' />%<br />
Ongoing: <txp:evaluate query='<txp:custom_field name="ongoing" /> * 100 div <txp:variable name="total" />' />%<br />
Rejected: <txp:evaluate query='<txp:custom_field name="rejected" /> * 100 div <txp:variable name="total" />' />%
</p>
</txp:article_custom>

I assume custom fields values are numeric and set, otherwise you’d need some precautions.

In the same way you can calculate 2 and 3.

Offline

#3 2019-01-13 07:40:16

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: how to calculate custom_fields of articles

This looks very interesting!!!

Is there a way to get the number of articles containing a particular value in a custom field? I have tried the snippets below but returned no results.

<txp:article_custom section="blog" limit="20000" form="">
<txp:evaluate query='<txp:custom_field name="Type" value="info" />' />
</txp:article_custom>
<txp:article_custom section="blog" Type="info" limit="20000" form="">
<txp:evaluate query='<txp:custom_field name="Type" value="info" />' />
</txp:article_custom>
</txp:if_logged_in>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#4 2019-01-13 09:11:01

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: how to calculate custom_fields of articles

Yiannis, I think you’re nearly there with your second example but you’d need to set a counter variable, update that by on with txp:evaluate in your loop and output it afterwards.

But as of txp 4.7, I think you should be able to use Oleg’s pgonly trick to replace what was formerly done with mdn_count:

<txp:article_custom section="blog" Type="info" pageby="1" pgonly />

It’s actually calculating the number of pages you need for a list, but with pageby="1", i.e. 1 article per page, it returns the number of articles for the given criteria.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2019-01-13 09:22:46

ax
Plugin Author
From: Germany
Registered: 2009-08-19
Posts: 165

Re: how to calculate custom_fields of articles

Perhaps this and similar problems could be resolved by introducing a txp:count tag to textpattern, like this:

<txp:count type="articles/images/links/files" category="..." section="..." />

Workarounds such as outlined above are not really intuitive solutions.

Offline

#6 2019-01-13 11:11:24

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: how to calculate custom_fields of articles

Hi Julian,

That works just fine!

I kind of agree with ax, but maybe we should not see these as workarounds but more as features of the article tags.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#7 2019-01-13 11:13:59

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: how to calculate custom_fields of articles

Yiannis, it could be

<txp:variable name="counter" add='<txp:if_custom_field name="Type" value="info" />' />

inside the article loop.

Offline

#8 2019-01-13 20:54:43

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: how to calculate custom_fields of articles

etc wrote

add=’<txp:if _custom_field name=“Type” value=“info” />’

I know I’ve not kept up with the current tag improvements and capabilities. Hence short question: Is that if intended?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#9 2019-01-13 22:20:03

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: how to calculate custom_fields of articles

Hi Uli,

yes, if is intended. Used as single tag, <txp:if_something /> returns 1 or nothing.

FWIW, <txp:custom_field /> does not accept value attribute.

Offline

#10 2019-01-13 22:51:09

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: how to calculate custom_fields of articles

Good question Uli, good answer Oleg!

Learned something new yet again!


TXP Builders – finely-crafted code, design and txp

Offline

#11 2019-01-14 11:34:03

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: how to calculate custom_fields of articles

Thanks, Oleg.

These amendments deserved being reflected in the docs: Currently, the if_cf page states that tag is a container tag only and the application as 1/0 test isn’t there, either. The code example might well be taken from your post, I think.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#12 2019-01-14 15:45:53

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: how to calculate custom_fields of articles

uli wrote #316109:

These amendments deserved being reflected in the docs

I’ve added the example and mentioned use as a single tag. Good catch, thanks Uli.

EDIT: this is actually the case for all (at least most?) conditional tags now: they can be used as a single as of 4.7.0, so we should probably update the docs accordingly for all of them.

Last edited by Bloke (2019-01-14 15:51:22)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

Board footer

Powered by FluxBB