bevc Posted June 6, 2017 Share Posted June 6, 2017 I'm having a problem with a foreach loop that isn't doing what I need it to. I want to take an array of id numbers, fetch the figures related to that id number from my mysql db and sum them. Initially the problem was that my loop was 'overwriting itself' so that the only summing it did was of the last id number. Then after I fixed that, although I have used a 'unique array', it seems that the loop is still summing everything, ie if the array contains the same id number 8 times, it loops through and multiplies that figure x8 (at least I think that's what it's doing). I'm not sure why while($row_outputs=mysqli_fetch_array($run_projects)){ $prog_name = $row_outputs['prog_name']; $proj_name = $row_outputs['name']; $projectId = $row_outputs['id']; $proj_array = array($projectId); $proj_array_uni = array_unique($proj_array); $projecty = ''; foreach($proj_array as $projectile){ $projectile = trim($projectile); $projecty .= $projectile; $get_outputs_active = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='1'"; $run_outputs_active = mysqli_query($conn, $get_outputs_active); while ($row_outputs_active =mysqli_fetch_array($run_outputs_active)){ $total_outputs_active += $row_outputs_active['total_cost']; } $ftotal_outputs_active = number_format($total_outputs_active,2); $get_outputs_run = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='2'"; $run_outputs_run = mysqli_query($conn, $get_outputs_run); while ($row_outputs_run =mysqli_fetch_array($run_outputs_run)){ $total_outputs_run += $row_outputs_run['total_cost']; } } ?> Can anybody see what my problem is? Thanks. Link to comment Share on other sites More sharing options...
Necuima Posted June 12, 2017 Share Posted June 12, 2017 You are missing a closing brace. To help diagnose issues like this, I always line my opening and closing braces up vertically so I can see where they start and end. Then, indent subservient routines also lining up the opening and closing braces. For me anyway, it helps to see what code loops are embedded in other loops, etc. Hope it helps. 1 Link to comment Share on other sites More sharing options...
Necuima Posted June 12, 2017 Share Posted June 12, 2017 (edited) for some reason my indents are not displaying but it may help anyway - hope so. while($row_outputs=mysqli_fetch_array($run_projects)) { // opening while loop $prog_name = $row_outputs['prog_name']; $proj_name = $row_outputs['name']; $projectId = $row_outputs['id']; $proj_array = array($projectId); $proj_array_uni = array_unique($proj_array); $projecty = ''; foreach($proj_array as $projectile) { $projectile = trim($projectile); $projecty .= $projectile; $get_outputs_active = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='1'"; $run_outputs_active = mysqli_query($conn, $get_outputs_active); while ($row_outputs_active =mysqli_fetch_array($run_outputs_active)) { $total_outputs_active += $row_outputs_active['total_cost']; } // end while 2 $ftotal_outputs_active = number_format($total_outputs_active,2); $get_outputs_run = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='2'"; $run_outputs_run = mysqli_query($conn, $get_outputs_run); while ($row_outputs_run =mysqli_fetch_array($run_outputs_run)) { $total_outputs_run += $row_outputs_run['total_cost']; } // end while 3 } // end foreach } // end opening while loop ?> Edited June 12, 2017 by Necuima 1 Link to comment Share on other sites More sharing options...
bevc Posted June 13, 2017 Author Share Posted June 13, 2017 You are missing a closing brace. To help diagnose issues like this, I always line my opening and closing braces up vertically so I can see where they start and end. Then, indent subservient routines also lining up the opening and closing braces. For me anyway, it helps to see what code loops are embedded in other loops, etc. Hope it helps. It wasn't that, but thank you very much for the reply. I also realised that I put the wrong array in the foreach, but that didn't fix it either! I can't figure it out, so I've gone back to the drawing board to try something else. Cheers. Link to comment Share on other sites More sharing options...
Recommended Posts