Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Get number of posts and do a little bit of calculation with it
Ok, here’s a question I bet you’ve never heard before :-)
I’m putting together some kind of hand-made Tumblr-blog for a personal site, and my layout basically exists out of 4 columns with all kinds of little notes/quotes/pictures/…
For the archive, I need to do the following:
- Get the total number of posts I made during a specific month (they’re posted in different sections)
- Divide this number with 4
- Add 1 to this number
This is the number of items that need to be shown in columns 1, 2 and 3. Column 4 needs to display all that’s left.
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline
Re: Get number of posts and do a little bit of calculation with it
Mary or Ruud will probably jump in here with a great TXP related way to do this, but personally I would simply just use some PHP to connect to the database and grab this data. Not the most elegant way but it will do the trick.
Sam Brown
sambrown.me | welovetxp.com
Offline
Re: Get number of posts and do a little bit of calculation with it
My initial thought was also to go for PHP. But, I’m just a simple designer, and I can’t write a single line of PHP :-)
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline
Re: Get number of posts and do a little bit of calculation with it
I think you’d get duplicate articles due to the “add 1 to this number” part.
The way to tackle this is probably:
- count the number of articles that match the criteria (= TOTAL)
- divide that number by 4. The result is a floating point number I’ll call X.
- first column: get results 1 to ceil(X), where ceil(X) = X rounded up to the nearest integer (if X is already an integer, it stays the same. In that case all 4 columns get the same amount of articles)
- second column gets results ceil(X+1) to ceil(2*X)
- third column gets results ceil(2*X+1) to ceil(3*X)
- fourth column gets results ceil(3*X+1) to TOTAL (where total is equal to 4*X)
And yes, this requires some PHP
Offline
Re: Get number of posts and do a little bit of calculation with it
Ruud: I would like to use the offset
and limit
attributes of <txp:article_custom />
.
Example (if a certain criteria has 15 articles):
- Column 1
<txp:article_custom offset="0" limit="4" />
(15/4=3.75 becomes 4) - Column 2
<txp:article_custom offset="4" limit="4" />
- Column 3
<txp:article_custom offset="8" limit="4" />
- Column 4
<txp:article_custom offset="12" limit="3" />
(maybe I don’t even have to specify alimit
for this one, as it has to display all articles that are left inside the criteria.
Any idea how the PHP would look like to get these numbers?
Last edited by maxvoltar (2008-05-09 09:14:28)
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline
#6 2008-05-09 09:11:08
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,306
Re: Get number of posts and do a little bit of calculation with it
Exciting subject!
I’ve often wanted a plugin that’s capable of doing some maths with db values.
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: Get number of posts and do a little bit of calculation with it
maxvoltar wrote:
Ruud: I would like to use the
offset
andlimit
attributes of<txp:article_custom />
.
Despite not being Ruud, I’ll try to sketch a solution.
This gives you the values for various offset
values you’d feed into the four <txp:article_custom />
tags which populate your four columns:
<txp:php>
global $offset_columns;
$posts = safe_field('count(*)', 'textpattern', 'DATE_SUB(NOW(), INTERVAL 1 MONTH) <= Posted');
$quarter = intval($posts / 4);
$offset_columns[0] = 0;
$offset_columns[1] = $quarter;
$offset_columns[2] = $quarter * 2;
$offset_columns[3] = $quarter * 3;
</txp:php>
There are various methods to splice these values into the tag handling functions for <txp:article_custom />
. One stub of a solution:
<txp:php>
global $offset_columns;
$month = safe_strftime('%Y-%m');
echo article_custom (
array(
'offset' => $offset_columns[1], // or [0], or [2], [3]
'limit' => 4,
'month' => $month,
'# more 'attribute' => 'value' pairs as required
)
)
#..and so on...
</txp:php>
Last edited by wet (2008-05-09 10:03:36)
Offline
Re: Get number of posts and do a little bit of calculation with it
wet you are gold! Are you taking the roundup into account?
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline
Re: Get number of posts and do a little bit of calculation with it
I think Ruud is perfectly right – I also see no need to roundup by 1.
Offline
Re: Get number of posts and do a little bit of calculation with it
wet: I agree with Ruud that there isn’t any need for a +1, but do think it would be handy to have a round up to the nearest integer (only up though)
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline
Re: Get number of posts and do a little bit of calculation with it
Hmmm.
This part:
$quarter = intval($posts / 4);
truncates the fractional part and leaves the integer value of the division by 4, so e.g. values from 3.0 to 3.9999 are truncated to 3. I think this works.
If you’d rather use commercial rounding, use:
$quarter = intval($posts / 4 + 0.5);
Offline
Re: Get number of posts and do a little bit of calculation with it
Thanks a lot! Now I can focus on the design. I assure you’ve never seen anything like it :-)
Textpattern projects: Maxvoltar, Made by Elephant, Twunch
Offline