i have foreach loop below.
i'm looking minus $wrap->wrap_total 1 came before in loop , struggling!
foreach ($wrap_query->result() $wrap) { echo date("d d m y", strtotime($wrap->wrap_date))." - "; echo money_format('%n', $wrap->wrap_total)." - "; echo "<br />"; }
current output below:
sun 07 aug 2016 - £10,000.00
sat 06 aug 2016 - £12,000.00
fri 05 aug 2016 - £8,000.00
what i'd is:
sun 07 aug 2016 - £10,000.00 (-£2,000)
sat 06 aug 2016 - £12,000.00 (£4,000)
fri 05 aug 2016 - £8,000.00
any , appreciated!
the commented code below might started. demo can found here well.
<?php setlocale(lc_monetary, 'en_gb'); // let imagine moment $wrap_query->result() contains // following simulated data mimicking data structure... $simulatedwrap1 = new stdclass(); $simulatedwrap2 = new stdclass(); $simulatedwrap3 = new stdclass(); $simulatedwrap1->wrap_date = "2016-08-07"; $simulatedwrap1->wrap_total = 10000.00; $simulatedwrap2->wrap_date = "2016-08-06"; $simulatedwrap2->wrap_total = 12000.00; $simulatedwrap3->wrap_date = "2016-08-05"; $simulatedwrap3->wrap_total = 8000.00; $rwrapcollection = [ $simulatedwrap1, $simulatedwrap2, $simulatedwrap3, ]; // make copy of main array collection // used within loop move array cursor // next element within collection $rwrapclone = $rwrapcollection; // fun: explicitly move cursor first // element in collection current($rwrapclone); // create , initialize variable $output empty string. // variable hold html content generated within loop $output = ""; foreach ($rwrapcollection $index=>$wrap){ // move cursor next item & catch value in variable $nextwrap = next($rwrapclone); $output .= date("d d m y", strtotime($wrap->wrap_date)) . " - "; $output .= money_format('%n', $wrap->wrap_total); // if there still next item, calculate // difference between current & next item if($nextwrap){ $difference = $wrap->wrap_total - $nextwrap->wrap_total; $output .= " (" . money_format('%n',$difference) . ")"; } $output .= "<br />"; } echo $output; //produces: sun 07 aug 2016 - £10,000.00 (-£2,000.00) sat 06 aug 2016 - £12,000.00 (£4,000.00) fri 05 aug 2016 - £8,000.00
effective code relates unique case:
<?php $output = ""; $resultscopy = $wrap_query->result(); current($resultscopy); foreach ($wrap_query->result() $wrap) { $nexwrap = next($resultscopy); $output .= date("d d m y", strtotime($wrap->wrap_date)) . " - "; $output .= money_format('%n', $wrap->wrap_total); if($nexwrap){ $difference = $wrap->wrap_total - $nexwrap->wrap_total; $output .= " (" . money_format('%n',$difference) . ")"; } $output .= "<br />"; } echo $output;
Comments
Post a Comment