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: 237

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: 3,043
Website

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.


etc_[ query | search | pagination | date | tree | cache ]

Offline

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

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 7,050
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github

Offline

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

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 3,253
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
Member
From: Germany
Registered: 2009-08-19
Posts: 149

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: 7,050
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github

Offline

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

etc
Developer
Registered: 2010-11-11
Posts: 3,043
Website

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.


etc_[ query | search | pagination | date | tree | cache ]

Offline

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

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

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: 3,043
Website

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.


etc_[ query | search | pagination | date | tree | cache ]

Offline

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

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 3,253
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

Board footer

Powered by FluxBB