array_sum Qustion

Hi Chaps,

I’m after a bit of help with summing up some arrays, to build a Quote for a Project.

  • Project_ID
    • Job_ID (repeated for each Project_ID)
    • Task_ID (repeated for each Job_ID)

Each Task_ID has it’s own cost, so what I need to do is sum up the Tasks, to give a total for Job and likewise I need to sum up the Job totals to give a Project total.

Hopefully this will give you some idea as to what I have in my database:

Project_1

Job_1 (fk_proj_id=1)

	Task_1 (fk_job_id=1)
	Task_1_cost = £500

	Task_2 (fk_job_id=1)
	Task_2_cost = £500

Job 2 (fk_proj_id=1000)

	Task_2 (fk_job_id=2)
	Task_2_cost = £900

The result I’m after is:
Job_1 = £1000
Job_2 = £900
Project_1 = £1900

Hmm you need to post your script excerp the for loop is access arrays once you have them you can add multiply or do anything you want with them.

if you already have the data just add them together and print accordingly.

As a pointer to calculate costs you could do a for loop

and inside other for loops to calculate each job (and task components)

Something like:

# Array to simulate DB info
$projects = array(
	1 => array(
		'jobs' => array(
			1 => array(
				1 => 500,
				2 => 500
			),
			2 => array(
				2 => 900
			)
		)
	),
	2 => array(
		'jobs' => array(
			1 => array(
				1 => 200,
				2 => 350
			),
			2 => array(
				2 => 1100,
				1 => 160
			)
		)
	)
);

$project_totals	= array();
$job_totals		= array();

foreach ($projects as $projectID => $project)
{
	foreach ($project['jobs'] as $jobID => $jobTasks)
	{
		$job_totals[$projectID][$jobID] = array_sum($jobTasks);
	}
	
	$project_totals[$projectID] = array_sum($job_totals[$projectID]);
}

print_r ($job_totals);
print_r ($project_totals);

?

Yeah, I already have the data, but i’ve tried using:

$task_total[$job_id] = $task_cost;

then

$job_total = array_sum($task_total);

but this basically gives me the Project total, the [$job_id] bit doesn’t seem to seperate the $task_totals, is there anything obvious I’m missing?

Hi, thanks for the link, I’ve checked it out, but it’s not really sticking . . .if I use a for loop, rather than storing each $task_cost in an array, how would I get a total for a job / project?

look up the for loop in the manual http://uk2.php.net/manual/en/control-structures.for.php

thanks for the code, however, I’m getting in a bit of a muddle, could you guide me through this.

I have the Jobs/Tasks in a loop but don’t know how to populate the array in the correct way.

I have:

<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th>// Project ID</th>
    <td></td>
  </tr>
<?php do { ?>
  <tr>
    <th>//Job ID</th>
    <td></td>
  </tr>
  <?php do { ?>
  <tr>
    <th>// Task ID</th>
    <td>// Task Cost ($task_cost)</td>
  </tr>
<?php } while ($row_rsTasks = mysql_fetch_assoc($rsTasks)); ?>
  <tr>
    <th></th>
    <td>// Job Total</td>
  </tr>
<?php } while ($row_rsJobs = mysql_fetch_assoc($rsJobs)); ?>
  <tr>
    <th></th>
    <td>// Project Total</td>
  </tr>
</table>

And need the PHP code you posted to work with this . . .is this possible?

Hi roscoe, thanks for the reply, is there an example you could show me?