We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Doing Simple Math from Entries, PHP order, etc.

Development and Programming

tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

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!

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
17 years ago
Sue Crocker's avatar Sue Crocker

rounds is stored not as a number, but as a string. All fields in the exp_weblog_data table are stored as text fields.

There’s probably a method for converting the text string to a number.

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Sue, thanks for that missing element. I’ll change the plugin and see what happens.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Um, you have php on output, yes?

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Indeed. Tried it both on input and output, same results.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

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;
       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

butcher man,

Thanks for the code. What does it do?

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
17 years ago
Lisa Wess's avatar Lisa Wess

I’ve moved this up to how-to. If you’re creating a plugin and need help with that, though, let me know and I can move it down to the plugin technical support forum. I just wasn’t sure where you were at with this now. =)

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

ah, lol…

Here, take this plugin and give it the parameters field_id=”” and weblog_id=”“. It should return the sum of the filed you give it. There is also a status=”” param should you want to limit to sertain statuses (default open)

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

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)

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton
If you’re creating a plugin and need help with that, though, let me know and I can move it down to the plugin technical support forum.

Maybe this should be moved to the plugin tech support.

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

I just realized that was the wrong version of the plugin. I just put in the right version in the previous post.

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Anybody out there? Haven’t heard back on this for a week or so. Help!

       
Mike Essl's avatar
Mike Essl
8 posts
17 years ago
Mike Essl's avatar Mike Essl

(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.

       
Mike Essl's avatar
Mike Essl
8 posts
17 years ago
Mike Essl's avatar Mike Essl

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);
                
      
            }
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.