I'm trying to distribute an amount proportionately to each row of a table based on the value of a column, 'value' and it's percentage of the sum of that column for all rows. For example
AmounttoDistribute = 200
CurrentTotal = 100
Row 1 value = 25
Row 2 Value = 50
Row 3 Value = 15
Row 4 Value = 10
I want to make it where
Row 1 will receive 50
Row 2 will receive 100
Row 3 will receive 30
Row 4 will receive 20
I've tried a couple different while loops but so far the results are very different than what I'm trying to achieve. I've already got the $AmounttoDistribute and $SumofValue in variables which I know to be correct. Here's what I've tried:
$val = mysqli_query($db, "SELECT Value FROM Table WHERE columnX = 1 AND columnY = 1");
$Result = mysqli_fetch_array($val);
$value = $Result['Value'];
while($Result = mysqli_fetch_array($val)){
mysqli_query($db, "UPDATE Table SET Value = Value + (Value / $SumofValue * $AmounttoDistribute) WHERE columnX = 1 AND columnY = 1");
I've also tried the while loop below where I already have a variable for AmounttoDistribute divided by SumofValue, ($AmountPerEach1):
$val = mysqli_query($db, "SELECT Value FROM Table WHERE columnX = 1 AND columnY = 1");
$Result = mysqli_fetch_array($val);
$value = $Result['Value'];
while($Result = mysqli_fetch_array($val)){
mysqli_query($db, "UPDATE Table SET Value = Value + (Value * $AmountPerEach1) WHERE columnX = 1 AND columnY = 1");
Thanks for any help