Array_sum counting all records in query result, not each row separately

hair pulling now

I have a query that takes into account all the corresponding $ price values ​​in an array Basically the original query checks the table on jobs that are completed but not posted Second query (inside the loop of the first query) gets all the items that need to be added (these values ​​are inside another tables (master class elements) and checked against the values ​​of the $ item array

the sum is calculated fine, I think it has something to do with where the $ total is being summed, since its addition ALL returns the totals, not the individual totals of the rows

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>

      

0


source to share


1 answer


If you mean that each row do while

should be a different total then when starts do

, set $price = [];

either $price = array();

or null

, because you will have all previous prices added as the final price, if your final price is for each request do while

and not for the hole, do What I am saying.



Remember to do $total +=

, not $total =

because you will overwrite the variable you are using outside of the main loop so that you end up with the wrong amount.

+1


source







All Articles