So I am wanting to do some simple math:
In this case add some numbers in an array:
<?php
$arr = array(1,2,3,4,5);
$sum = 0;
for ($i = 0; $i < count($arr); $i++)
$sum += $arr[$i];
print ($sum);
?>
This works fine. But when I try to get the array populated with EE data, no workie, i just get a blank page.
$arr = array({exp:weblog:entries weblog="rounds"}{rounds},{/exp:weblog:entries});
I can’t tell if you can have EE tags WITHIN php or not.
I then resorted to making a quick plugin. Again works with hard coded data:
{exp:simple_math:add}
1,2,3,4,5,
{/exp:simple_math:add}
However, it doesn’t work if there is a weblog entry inside that plugin tag.
{exp:simple_math:add}
{exp:weblog:entries weblog="rounds"}{rounds},{/exp:weblog:entries}
{/exp:simple_math:add}
Help!
Perhaps, if you’re just doing a simple sum of fields, you might want to use just a plugin instead…
global $DB;
$query = $DB->query("SELECT d.field_id_n FROM exp_weblog_data AS d LEFT JOIN exp_weblog_titles AS t ON t.entry_id = d.entry_id WHERE d.weblog_id = 'n' AND t.status = 'open'");
if($query->num_rows >= 1)
{
$values = array();
foreach($query->result AS $row)
{
$values[] = (int) $row['field_id_n'];
}
return array_sum($values);
}
return false;
Butcher Dude,
Let me explain what I am doing. I have a little utility for members that the enter the number of rounds of golf they played and the date, and then I give a monthly report.
I’ve got one weblog with custom fields for the number of rounds, hence “rounds”. So I’ve got to allow folks to pull a report for any month which shows with a simple url_permalink path. Because of this I don’t think the sum_fields pi that you just did will work for this application.
So back to my plugin, I had my friend right me that simple math plugin because I know I would use it a bunch (plus would be good for other EE folks). Everything “looks” like it should work but doesn’t. The plugin does add numbers, just not when I give it numbers from EE fields.
For some specifics, see the first entry in my post. Also I’ve included the plugin here: (updated version 1)
(Sorry for the double post, I had two windows open…)
Daniel Walton, is there any reason why the sum_of_fields plug in would not work on the lasted install of EE?
I’m calling it like this:
{exp:sum_of_fields field_id="minutes" and weblog_id="episodes"}
Am I missing something?
Thank you.
Hi Daniel Walton, I know this thread is old news, but for the life of me I can’t figure out how to make your sum_of_fields plug in work. I also realize that you probably have no intention of supporting it! (sorry)
I’m calling it like this: {exp:sum_of_fields field_id="10" and weblog_id="3"}
and I get this: Notice: Undefined index: field_id_n in /home/.dorrie/mikeessl/essl.tv/clicker/plugins/pi.sum_of_fields.php on line 35
Line 35 is this: $values[] = (int) $row[‘field_id_n’];
I’m don’t know enough php/mysql to sort this out myself, any help would be appreciated as this is exactly what I need to do.
UPDATE: I got the plugin to work with the following replacement:
if($query->num_rows >= 1)
{
$values = array();
foreach($query->result AS $row)
{
$values[] = (int) $row['field_id_'.$field_id];
}
$this->return_data = array_sum($values);
}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.