Jump to content

Adding total field amounts based on child fields


Galaxy
 Share

Recommended Posts

getting there but...

http://legacytrust.acumulus.co.nz/recipient-list/

this is the complete list code

        echo "<ul class='nav'>";
    foreach($result as $child) {
        echo "<li>";
        echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
           $image = $child->logo;
            if (count($child->logo)){
                echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
            }
            else{
                echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
            }
           
   
        $dollars = $pages->find("grants.amount_approved!=''");

        foreach ($dollars as $dollar) {

              $u = $dollar->grants;
              $total = 0;

              foreach ($u as $z) {

               $total += $z->amount_approved;
              }
                echo "Total grants: <span>\${$total}</span>";
            }
            
         echo "</li>";
    }
    echo "</ul>";
 
Link to comment
Share on other sites

Well, that's not what I am seeing on my tests :).

1. What do you get when you ONLY run the grants total code, i.e. removing all other code but that one?

2. Try placing the grants foreach outside the result foreach like so:

echo "<ul class='nav'>";
foreach($result as $child) {
   echo "<li>";//wrong?
   echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
   $image = $child->logo;
   if (count($child->logo)){
           echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
   }
   else{
           echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
   }
      
}
	
//grants = name of repeater; grants = name of an integer field in the repeater "grant"
$dollars = $pages->find("grants.amount_approved!=''");//grab pages whose repeater field "grants" value are more than 0

foreach ($dollars as $dollar) {

     $u = $dollar->grants;
     $total = 0;

     foreach ($u as $z) {

            $total += $z->amount_approved;
      }
echo "Total grants: <span>\${$total}</span>";
}
            
echo "</li>";//wrong?
echo "</ul>";

Your <li>s also seem to be in the wrong place; but that's for later..

Link to comment
Share on other sites

And you want the sum of all those that appear? If so try this:
 

$dollars = $pages->find("grants.amount_approved!=''");
$total = 0;

foreach ($dollars as $dollar) {

    foreach ($dollar->grants as $z) {
        $total += $z->amount_approved;
    }
}

echo "Total grants: <span>\${$total}</span>";
echo "</li>";

Edit: damn code editor... had to fix some fix the quirks

Edit2: Just one question. Why don't you use a table for this? People used them for years in situations where they shouldn't be used, but would be nice to see them in situations where they are supposed to be used :)

Link to comment
Share on other sites

And you want the sum of all those that appear? If so try this:

$dollars = $pages->find("grants.amount_approved!=''");
$total = 0;

foreach ($dollars as $dollar) {

    foreach ($dollar->grants as $z) {
        $total += $z->amount_approved;
    }
}

echo "Total grants: <span>\${$total}</span>";
echo "</li>";

Edit: damn code editor... had to fix some fix the quirks

@Diogo,

That's what I initially thought he wanted; see my previous code on page 1 of this thread. He does not want the GRAND total across the site. He wants sub-totals, so to speak, e.g.

Acorn $599 (total for this recipient from various sources [sum from several repeater fields on the Acorn page])

Evans Road church: $500 ([ditto])

In other words, he wants the totals for each recipient (which of course, add up to the GRAND total) ;)

Link to comment
Share on other sites

Still, if the echo "Total grants:..." is inside the deepest foreach, there will always be dozens of "Total grants:..." in each <li>

Edit: For the total in the end creating another variable should do the job:

// before the first foreach:
$finaltotal = 0;
$dollars = $pages->find("grants.amount_approved!=''");
$total = 0;

foreach ($dollars as $dollar) {

    foreach ($dollar->grants as $z) {
        $total += $z->amount_approved;
    }
}

echo "Total grants: <span>\${$total}</span>";
echo "</li>";

$finaltotal += $total;
// in the end
echo $finaltotal;

I didn't read all the thread carefully, so if this doesn't make any sense, please ignore me :)

Link to comment
Share on other sites

Still, if the echo "Total grants:..." is inside the deepest foreach, there will always be dozens of "Total grants:..." in each <li>

That's why I suggested to move the grants foreach outside the result foreach. I don't see its use inside the very first (result foreach)  :)...I could be wrong though ;)

Link to comment
Share on other sites

My head is spinning :)  and I'm sure my explanations are being misinterpreted somehow and I apologise, but here goes...

On an individual page (eg. http://legacytrust.acumulus.co.nz/recipient-list/1st-otumoetai-scouts/ )

You'll see how it has 2 grants (with their dates) and their total

1. 1 August 2012 $1000

2. 1 Junwe 2013 $500

Total grants: $1500

This is fine and is using the following

     

        echo "<ol>";
        foreach ($page->grants as $grant) {
          echo "<li>{$grant->cheque_date} ";
          echo "<span>\${$grant->amount_approved}</span></li>";
          $total += $grant->amount_approved;
        }
        echo "</ol>";
        echo "<h3>Total grants: <span>\${$total}</span></h3> ";
        $last_cheque_date = $page->grants->sort('-cheque_date')->first()->cheque_date;
          echo "<p>The most recent cheque date is <span>{$last_cheque_date}</span>.</p>";
 

Now, go back to the  Recipient list http://legacytrust.acumulus.co.nz/recipient-list/

All I want to do is have the total of each individual's grants within its own <li> after the link to its own page and the icon to show if a logo has been uploaded.

Link to comment
Share on other sites

Galaxy,

You haven't answered my questions above :) Have you tried to move the "grants foreach" outside the "result foreach" as I suggested above? Have you tried independently running the "grants foreach" as I suggested, in order to test the code?. It works for me here. At the moment, do not worry about the <li>s. The most important thing is to get the totals right, I think...

Edited by kongondo
Link to comment
Share on other sites


// taking your code from some posts ago:

foreach($result as $child) {

    echo "<li>";
    echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
    $image = $child->logo;
    if (count($child->logo)){
        echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
    }
    else{
        echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
    }

    // and from here on do the same as you did in the individual page, but adapted ($page is now $child)
    
    $total = 0;

    foreach ($child->grants as $grant) {
          $total += $grant->amount_approved;
    }

    echo "Total grants: <span>\${$total}</span>";
    echo "</li>";

}
  • Like 1
Link to comment
Share on other sites

Kongondo if I do like you suggest

2. Try placing the grants foreach outside the result foreach like so:

No totals appear so I've got

    echo "<ul class='nav'>";
     echo "<li>";
        foreach($result as $child) {
      
        echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
           $image = $child->logo;
            if (count($child->logo)){
                echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
            }
            else{
                echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
            }
         
                
         $dollars = $pages->find("grants.amount_approved!=''");

        foreach ($dollars as $dollar) {

              $u = $dollar->grants;
              $total = 0;

              foreach ($u as $z) {

               $total += $z->amount_approved;
              }
                echo "Total grants: <span>\${$total}</span>";
            }      
    }
    
    echo "</li>";
    echo "</ul>";
 

note: moved the <li> to outside all foreach

I feel like it's getting close... might be the 4th coffee this morning...



kongondo

OK, are those totals correct? E.g., is the total for YMCA = 2000?

Yes!

Link to comment
Share on other sites

Galaxy, you are too concerned about the html tags and I think that's what's confusing you. Listen to kongondo's advice: think of the content; understand how things get grouped; and only then add the tags into the right places.

  • Like 1
Link to comment
Share on other sites

Galaxy,

See Diogo's code above. That should work for you. My problem was that I suggested code "independent" of your "result foreach" (I was starting with the $dollars = blah blah), which you don't need if you use your "result foreach" as Diogo suggests.. :).....I'll eat my hat if Diogo's code doesn't work...I'm sure it will!

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Hallelujah diogo! And, thanks Kongondo for perservering with this. This indeed works:



 

// taking your code from some posts ago:

foreach($result as $child) {

    echo "<li>";
    echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
    $image = $child->logo;
    if (count($child->logo)){
        echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
    }
    else{
        echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
    }

    // and from here on do the same as you did in the individual page, but adapted ($page is now $child)

$total = 0;

    foreach ($child->grants as $grant) {
          $total += $grant->amount_approved;
    }

    echo "Total grants: <span>\${$total}</span>";
    echo "</li>";

}
  • Like 1
Link to comment
Share on other sites

added, and all good. Thanks guys. Love your work.

    foreach($result as $child) {

    echo "<li>";
    echo "<a href='{$child->url}' class='list-title'>{$child->title}</a>";
    $image = $child->logo;
    if (count($child->logo)){
        echo "<span style='padding-right:140px;'><a href='{$child->logo->url}'><img src='/site/assets/uploads/yes.png'></a></span>";
    }
    else{
        echo "<span style='padding-right:140px;'><img src='/site/assets/uploads/no.png'></span>";
    }

    // and from here on do the same as you did in the individual page, but adapted ($page is now $child)
    $total = 0;
    foreach ($child->grants as $grant) {
          $total += $grant->amount_approved;
    }

    echo "Total grants: <span>\${$total}</span>";
    echo "</li>";

}
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...