Update numbers in table with variable and update variable
As you can see from the table below the user with the id of 1 has 10
points altogether
What I'm trying to do is have one number submitted from a form
$edit_points to loop through and subtract from user 1
This number could be anything eg:
$edit_points = 6
would subtract 4 from the first record
then move to the next and only subtract the remainder 2
then break out of loop
UserID | TotalPoints
-------------------
1 | 4
1 | 4
1 | 2
4 | 3
3 | 3
5 | 4
This is what I've got so far
$pointLeft = $edit_points;//sent from form
while($point = mysqli_fetch_array($output)){
if($pointLeft > $point["TotalPoints"]){//if pointsLeft more than this record
$remainder = $pointLeft-$point["TotalPoints"];//get remainder
$query2 = "UPDATE pointstable SET TotalPoints=0 WHERE UserID=".$UserID;
$output2 = $mysqli->query($query2);
}elseif($pointLeft < $point["TotalPoints"]){
$remainder = $point["TotalPoints"] - $pointLeft;
$query2 = "UPDATE pointstable SET TotalPoints=".$remainder." WHERE
UserID=".$UserID;
$output2 = $mysqli->query($query2);
}
$pointLeft = $remainder;
}
It works to a point and seems to populate all the records with the same
last value that remainder holds I've tried break and putting an if
($pointLeft<=0) above the first if
but none works.
any help? or a better way of doing it preferably using php
No comments:
Post a Comment