Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 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 More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 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 More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 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 More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 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 More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 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 More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 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 More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 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 More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 (edited) 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 July 1, 2013 by kongondo Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 have you refreshed http://legacytrust.acumulus.co.nz/recipient-list/ It's showing all the individual items totals... Link to comment Share on other sites More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 OK, are those totals correct? E.g., is the total for YMCA = 2000? 1 Link to comment Share on other sites More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 // 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>"; } 1 Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 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 More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 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. 1 Link to comment Share on other sites More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 (edited) 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 June 30, 2013 by kongondo 2 Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 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>";} 1 Link to comment Share on other sites More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 Good! So I get to keep my hat! Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 Hold on a minute!! I think it's now adding up each and creating a running total... http://legacytrust.acumulus.co.nz/recipient-list/ see how the numbers combine and increase down the list Link to comment Share on other sites More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 2 Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 Good! So I get to keep my hat! No! See the above dude. You guys are funny. Love your work but I still have to tweak this... Link to comment Share on other sites More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 I added this line later: $total = 0; Do you have it in your code? 1 Link to comment Share on other sites More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 .... Yeah, where you place the $total = 0; determines whether you get cumulative totals, sub-totals or grand total 1 Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 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 More sharing options...
diogo Posted June 30, 2013 Share Posted June 30, 2013 You get your hat back 2 Link to comment Share on other sites More sharing options...
kongondo Posted June 30, 2013 Share Posted June 30, 2013 I like this one too! Pity I can't wear both; maybe alternate every other week, hehe Link to comment Share on other sites More sharing options...
Galaxy Posted June 30, 2013 Author Share Posted June 30, 2013 Thanks again. I feel like I owe ya something.... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now