Linked Questions

Popular Questions

how to get sum on every loop on the table

Asked by At

I have a problem on breaking down the amount of the given payment, I think i was missing something on my code. This is my table below.

If i have payment of 5,000, it will deduct on the first row and the remaining sum will send to the 2nd row.

//this is the payment

$payment = 5000;
$temp = '';

//the loop of function

$data = payment_breakdown();
foreach ($data as $row) {

    $amount = $row['AMOUNT'] - $payment."\n";
}

//handle the difference of every loop of the function

$temp = $amount;

//result of the amount

echo $amount;

//this is my function to get the data from the table

function payment_breakdown(){
    $db = database2();
    $query = "SELECT AMOUNT FROM TABLE1";
    $cmd = $db->prepare($query);    
    $cmd->execute();
    $rows = $cmd->fetchAll();
    $db = null;

    return $rows;
}

supposedly if the amount on the table is this

Data From Table | The Payment
        3000    |     3000
        3000    |     2000

Related Questions