Search code examples
phparray-sum

array_sum counting all records in result query not each row individually


pulling hair out now

I have a query that counts all relevant $price values in the array Basically the initial query checks the table to jobs that are completed but not invoiced The second query ( inside the initial query loop ) gets all the items that need adding up ( these values are found inside another table ( workshop-items ) and are checked against the $item array values

the total is being calculated ok , i think it has something to do with where the $total is placed as its adding up ALL returned totals not the individual row totals

code below

<ul class="list-group">
<?php 
    $uninvoicedq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
    $uninvoiced = mysqli_fetch_assoc($uninvoicedq);
    if($uninvoiced) {
        do { 
            // User Query
            $wscid = $uninvoiced['wscid'];
            $userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
            $user = mysqli_fetch_assoc($userq);
            $wtbdq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
            $wtbdr = mysqli_fetch_assoc($wtbdq);
                do {
                    $wtbd = explode(":",$wtbdr['worktobedone']);
                    foreach($wtbd as $item) 
                    {
                        $priceq = mysqli_query($con,"SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
                        $pricer = mysqli_fetch_assoc($priceq);

                        $price[] = $pricer['incvat'];
                        $items[] = $pricer['description'];
                        //echo $item.' - '. $pricer['incvat'].'<br>';
                    }

                    $total = array_sum($price);
                } while($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
    <li class="list-group-item text-right" style="border:none;" title="<?php echo $itemview;?>"><span class="badge pull-left" style="background-color:#F00;">Not Invoiced</span><?php echo '&pound;'.$total.' - '; echo $user['forename'].' '.$user['surname'].' - ' .$uninvoiced['summary'];?> </li>
<?


                $itemList = implode(":",$items);
                $itemview = str_replace(":","\n",$itemList);
?>

<?          } while($uninvoiced = mysqli_fetch_assoc($uninvoicedq));
        } else { 
            echo "No Jobs Waiting To Invoiced";
        }
?>
        </ul>

Solution

  • If you mean that each row of the do while should be different totals, then, when do begins, set $price = []; or $price = array(); or to null, because you will have all the previous prices added up as a final price, if your final price is for each query of the do while and not for the hole, do what I say.

    Remember to do $total += and not $total = because you will overwrite the variable that you are using outside the main loop, so you would get a wrong total.