Doing Simple Math from Entries, PHP order, etc. 
Posted: 07 November 2007 07:21 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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!

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 07 November 2007 07:37 PM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  10898
Joined  04-29-2002

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.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Most recent build notes - 20080808

Profile
MSG
 
 
Posted: 07 November 2007 07:41 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 07 November 2007 07:56 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1067
Joined  03-22-2006

Um, you have php on output, yes?

 Signature 

(a.k.a the_butcher)

Profile
 
 
Posted: 07 November 2007 07:59 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 07 November 2007 08:05 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1067
Joined  03-22-2006

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;

 Signature 

(a.k.a the_butcher)

Profile
 
 
Posted: 08 November 2007 12:50 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

butcher man,

Thanks for the code. What does it do?

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 08 November 2007 01:34 PM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  31067
Joined  05-14-2004

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. =)

 Signature 
Profile
MSG
 
 
Posted: 08 November 2007 02:00 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1067
Joined  03-22-2006

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)

File Attachments
pi.sum_of_fields.zip  (File Size: 1KB - Downloads: 123)
 Signature 

(a.k.a the_butcher)

Profile
 
 
Posted: 08 November 2007 04:13 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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)

File Attachments
pi.simple_math.php.zip  (File Size: 2KB - Downloads: 123)
 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 08 November 2007 04:14 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004
Lisa Wess - 08 November 2007 01:34 PM

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.

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 08 November 2007 04:22 PM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 19 November 2007 06:15 PM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  224
Joined  11-16-2004

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

 Signature 

T. Payton
OneCreative | Albuquerque, NM

Profile
 
 
Posted: 27 June 2008 04:21 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Avatar
Rank
Total Posts:  60
Joined  04-17-2002

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

Profile
 
 
Posted: 27 June 2008 05:24 PM   [ Ignore ]   [ # 14 ]  
Grad Student
Avatar
Rank
Total Posts:  60
Joined  04-17-2002

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

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 10:33 AM
Total Registered Members: 60714 Total Logged-in Users: 20
Total Topics: 73169 Total Anonymous Users: 11
Total Replies: 394631 Total Guests: 475
Total Posts: 467800    
Members ( View Memberlist )