Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Extract MAX value from custom_field
Hi all ;)
I want to convert a value stored in a custom field (number of views for the current page) to a “star rating”; the math formula is: current page views x 5 / Max value of views on the entire website
.
So I need to extract this MAX()
value. How to with a <txp:article_custom />
tag and maybe a FIELD()
attribute?
Thank you in advance for your help.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Extract MAX value from custom_field
That shouldn’t be difficult:
<txp:article_custom fields="max(cfname)" limit="0">
<txp:custom_field name="cfname" />
</txp:article_custom>
Online
Re: Extract MAX value from custom_field
Not sure if I’ve understood you correctly but could you not simply sort your articles by the pageviews custom field ordered by descending and limit output to the first one:
<txp:article_custom section="mysection" sort="custom_1+0 desc" limit="1">
<txp:variable name="pageviews_max"><txp:custom_field name="pageviews" /></txp:variable>
</txp:article_custom>
<txp:php>
global $variable, $thisarticle;
$variable['star_rating'] = round( (intval($thisarticle['custom_1']) * 5) / intval($variable['pageviews_max']), 1);
</txp:php>
replacing custom_1
and pageviews
with the custom field name and number you are using.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Extract MAX value from custom_field
etc wrote #332980:
That shouldn’t be difficult:
<txp:article_custom fields="max(cfname)" limit="0">...
Neat. The fields
attribute is new to me. It looks like it’s undocumented in the docs as yet.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Extract MAX value from custom_field
etc wrote #332980:
That shouldn’t be difficult:
<txp:article_custom fields="max(cfname)" limit="0">...
I tried this:
<txp:article_custom fields="max(custom_1)" limit="0">
<txp:custom_field name="pageviews" />
</txp:article_custom>
… I got… a list of corresponding custom field values but not the MAX()
one…
Trying this:
<txp:article_custom fields="max(pageviews)" limit="0">
<txp:custom_field name="pageviews" />
</txp:article_custom>
I got only one value but not the MAX()
one…
Last edited by Pat64 (2022-03-24 03:19:29)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Extract MAX value from custom_field
Online
Re: Extract MAX value from custom_field
jakob wrote #332982:
It looks like it’s undocumented in the docs as yet.
Offline
Re: Extract MAX value from custom_field
Fine.
Thank you, Oleg ;)
I’m using this with some good results (only one: the max value):
<txp:article_custom section='<txp:section />' fields="max(pageviews+0)" limit="1" sort="custom_2+0 desc">
<txp:custom_field name="pageviews" />
</txp:article_custom>
Note: pageviews
is my custom field name and custom_2
its column into the textpattern
table.
It seems obvious Oleg for all your advice given that you are one of the best users of Textpattern! A kind of magician.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Extract MAX value from custom_field
Pat64 wrote #332994:
I’m using this with some good results (only one: the max value):
<txp:article_custom section='<txp:section />' fields="max(pageviews+0)" limit="1" sort="custom_2+0 desc">...
What roles does the fields
attribute play there? If you leave it out, I’m guessing (without having tested it) that it should also output a single article in your section with the greatest value for custom_2.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Extract MAX value from custom_field
I did some tests on a local server with few articles. The code above seems to work. Unfortunately, not within a website in production… Still need some searches to make that working…
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Extract MAX value from custom_field
Pat64 wrote #332994:
It seems obvious Oleg for all your advice given that you are one of the best users of Textpattern!
That’s what usually happens after ~1300 commits, but thank you :-)
jakob wrote #332995:
What roles does the
fields
attribute play there? If you leave it out, I’m guessing (without having tested it) that it should also output a single article in your section with the greatest value for custom_2.
I concur, fields
is useless and unused here (and it must be sort="custom_2+0"
, not pageviews
). We could allow for expressions like max(cfname+0)
, but it feels ugly.
Online
Re: Extract MAX value from custom_field
Here is my poor solution for the moment (it’s ugly):
A list of all values:
<txp:variable name="max_values"><txp:article_custom section='<txp:section />' limit="0" sort="custom_2"><txp:custom_field name="pageviews" />,</txp:article_custom></txp:variable>
… and the max one:
<txp:php>
/**
* Rating value based on this
* page view and max views
*/
global $variable;
$max = max(explode(',', $variable['max_values']));
echo ceil(custom_field(array('name'=>'pageviews')) * 5 / $max);
</txp:php>
etc wrote #332997:
I concur,
fields
is useless and unused here (and it must besort="custom_2+0"
, notpageviews
). We could allow for expressions likemax(cfname+0)
, but it feels ugly.
Yes Oleg. Sorry, I didn’t try: only sort="custom_2+0"
is needed.
Last edited by Pat64 (2023-06-30 12:03:51)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Pages: 1