Jump to content

array_sum Qustion


kool_samule

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/204378-array_sum-qustion/
Share on other sites

Something like this then?

 

$projects = array(
	//Project ID
	1 => array(
		//Job ID
		1 => array(
			//Task ID
			1 => 500,
			2 => 500
			),
		//Job ID
		2 => array(
			//Task ID
			1 => 900
			)
		)
	);

$costs = array();
foreach($projects as $project => $job){
foreach($job as $id => $task){
	echo "Job " . $id . " = " . array_sum($task) . "<br />";
	$costs[$id] = array_sum($task);
}
echo "Project " . $project . " = " . array_sum($costs);
}

Link to comment
https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070341
Share on other sites

Hi ProjectFear, 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?

Link to comment
https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070385
Share on other sites

Hi jcbones, got a solution that works:

<?php

$job_cost = 0;

$proj_cost = 0;

do { ?>

  <tr>

    <th>//Job ID</th>

    <td></td>

  </tr>

  <?php do { ?>

  <tr>

    <th>// Task ID</th>

    <td>// Task Cost ($task_cost)</td>

  </tr>

<?php

  $job_cost += $task_cost;

} while ($row_rsTasks = mysql_fetch_assoc($rsTasks)); ?>

  <tr>

    <th></th>

    <td>// Job Total</td>

  </tr>

<?php

  $proj_cost += $job_cost;

} while ($row_rsJobs = mysql_fetch_assoc($rsJobs)); ?>

 

Thanks anyway

Link to comment
https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070708
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.