0

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>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Chris Yates
  • 65
  • 10
  • you recently asked the same question. Why did you accept an answer if it did not satisfy your requirements? – Rotimi May 17 '17 at 14:50
  • It did satisfy, this is a new problem he didn't know he had – matiaslauriti May 17 '17 at 14:50
  • its a new problem, the answer for the old question worked, this is a new problem but sort of involves the same question – Chris Yates May 17 '17 at 14:52
  • 1
    P0IT10n's answer should solve your problem. I would also highly recommend to initialize the `$price` and `$items` arrays before usage (which you may have done and just haven't posted the code). – SaschaM78 May 17 '17 at 14:59
  • @SaschaM78 you are right, it is not necessary but a good practice, it will raise a `notice` but nothing more if not defined. – matiaslauriti May 17 '17 at 15:15
  • 1
    @matiaslauriti you are absolutely right but imagine that somewhere else the same variable has been declared and used already, in that case you'd add elements to a prefilled array and get different results than you'd expect. – SaschaM78 May 18 '17 at 17:04
  • @SaschaM78 yes, but that is a developers error if that happens. You are the only one that knows that part really well, or at least, the function/method you are creating handling, so there should not be global variables/properties that would allow what you are saying. That would be a wrong approach or bad practices. – matiaslauriti May 18 '17 at 18:35
  • @matiaslauriti just one last comment from my side, don't want to start a lengthy discussion: if you are working in a larger team (which I do) where one developer works in several projects, it's critical to always initialize variables properly to not get side effects as mentioned above. You never know who else in the future may work on code you've written, that's why I'd recommend to follow standard coding rules as early as possible in your project. Just my final two cents :-) – SaschaM78 May 19 '17 at 07:56
  • @SaschaM78 yes yes, I do too work in a large team and yes. I just think that if you have good OOP concepts and experience, you will not have any problem if you don't initialize variables, everything should be in a method, OOP pilars, so, there should not be any global or initialized variable that you are going to overwrite or anything like that :P but yes, I agree. You should not have a method that is longer than 200 lines, and because it is in a method, nothing is going to be accesible from outside, except the method call itself and properties, but not variables. – matiaslauriti May 19 '17 at 13:03

1 Answers1

1

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.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43