Jump to content

Trying to display a summary table (from data already written) [accumulator?)


Recommended Posts

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

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.