JasonGreen Posted October 10, 2009 Share Posted October 10, 2009 I have pulled out a chronicle of data using 'include' (and strtok) and have displayed it in a table using a While loop. What I need to do is summarize that data. It would be great if foreach wasn't only for array's as I must keep my While loop. So, I basically just have generic table of data with assigned attributes to work with at the moment. The data is regarding computers (via serial number), number of times these computers were serviced, and the cost for each repair. Now I need to display a summary as there are only about 10 computers and many are listed multiple times. In this summary I want to display the total number of times serviced (repaired) and the total cost from all repairs for each computer each on a separate line. I feel I should be doing something like .... $cost = $cost + $cost echo $cost much like an accumulator but I haven't had any luck with that. I really just need to be pointed in the right direction. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/ Share on other sites More sharing options...
MadTechie Posted October 10, 2009 Share Posted October 10, 2009 Where is this data coming from ? you could always compile the data into an array ie <?php //Data from your source! $tmp = array( array(123,'John',10), array(456,'Fred',20), array(123,'John',30) ); $myArray = array(); //put data into array foreach($tmp as $t){ //New computer $serial = $t[0]; $Client = $t[1]; $Cost = $t[2]; if(!isset($myArray[$serial])) $myArray[$serial] = array('repairs' => 0, 'Client' => $Client, 'Cost' => 0); //add data $myArray[$serial]['repairs']++; $myArray[$serial]['Cost'] += $Cost; } //display data foreach($myArray as $serial => $a) { echo "[$serial] -> {$a['Client']} Spent \${$a['Cost']} on {$a['repairs']} repairs<br />\n"; } ?> [123] -> John Spent $40 on 2 repairs [456] -> Fred Spent $20 on 1 repairs Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/#findComment-934368 Share on other sites More sharing options...
JasonGreen Posted October 10, 2009 Author Share Posted October 10, 2009 Wouldn't an array require me to to define attributes within the array itself? I can't do that. My attributes are already defined and then diplayed using a While loop. I am just trying to display a summary of that which is allready displayed Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/#findComment-934378 Share on other sites More sharing options...
MadTechie Posted October 10, 2009 Share Posted October 10, 2009 I really don't understand what the problem is.. Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/#findComment-934386 Share on other sites More sharing options...
JasonGreen Posted October 10, 2009 Author Share Posted October 10, 2009 Sorry, perhaps that is my fault. I have displayed a table using a while loop and I want to keep that, I don't want to change it. I just want to add on to it a second table to summarize totals from my first table and I can't find a function that will do that. Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/#findComment-934402 Share on other sites More sharing options...
MadTechie Posted October 10, 2009 Share Posted October 10, 2009 Using the code i posted, take the code that's inside the foreach($tmp as $t){ loop and put that into your array (update as required) then use the //display data code to display it Quote Link to comment https://forums.phpfreaks.com/topic/177204-trying-to-display-a-summary-table-from-data-already-written-accumulator/#findComment-934428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.