kool_samule Posted June 10, 2010 Share Posted June 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/ Share on other sites More sharing options...
JasonLewis Posted June 10, 2010 Share Posted June 10, 2010 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); } Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070341 Share on other sites More sharing options...
kool_samule Posted June 10, 2010 Author Share Posted June 10, 2010 Uhhh, yeah, that gives us the results I'm after, just need to figure out how to populate the $projects array in the correct way . . . Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070373 Share on other sites More sharing options...
kool_samule Posted June 10, 2010 Author Share Posted June 10, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070385 Share on other sites More sharing options...
jcbones Posted June 10, 2010 Share Posted June 10, 2010 Post your full code. We can simplify the database calls, and get it to pull all the data with one loop to populate the array. Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070527 Share on other sites More sharing options...
kool_samule Posted June 11, 2010 Author Share Posted June 11, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204378-array_sum-qustion/#findComment-1070708 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.