在不使用codeigniter中的查询构建器的情况下总计总计

I would like to find total outstanding arrears for each doctor and display the result.

my controller

public function pay_salaries()
{ 
$salary['salaries'] = $this->db->query("SELECT * FROM salaries WHERE Payment_status IS NULL ORDER BY Salary_id DESC")->result_array();

$this->load->view('backend/admin/pay_salaries',$salary);
}

This controller really does so well until I want to echo all arrears for a doctor

view

    <thead>
        <tr>
            <th>DOCTOR'S NAME</th>
            <th>TOTAL PENDING AMOUNT</th>
        </tr>
    </thead>
    <tbody>
<?php foreach ($salaries as $salary): 

$doctor_id = $salary['doctor_id'];

$doctor_name = $this->db->query("SELECT * FROM user_details,doctors WHERE Doctor_id='$doctor_id' AND User_details_User_id=User_id")->row_array();

   $total_outstanding_amount = $this->db->query("SELECT SUM(Amount) AS Arrears FROM salaries WHERE Payment_status IS NULL AND doctor_id = '$doctor_id'")->result_array();
?>
    <tr>
            <td><?php echo $doctor_name['Fname'].' '.$doctor_name['Sname']; ?></td>
            <td><?php echo $total_outstanding_amount['Arrears']; ?></td>

        </tr>
<?php endforeach ?>   
    </tbody>

my database is like this enter image description here

total i expect in Arrears should be 5500+500 = 6000

I can't figure this out in a day. Please help... Thanks in advance

The error I keep getting is

Severity: Notice

Message: Undefined index: Arrears

Filename: admin/pay_salaries.php

Line Number: 79

Backtrace:

File: C:\wamp64\www\Alphadoctors\application\views\backend\admin\pay_salaries.php
Line: 79
Function: _error_handler

This should help you, as i could not figure out what you doing in your code.

so what you can do is set a var $sum = 0; outside the foreach loop. then you can try

foreach ($sql['Amount_CHANGEME'] as $key => $value)
{
    $sum += $value
    echo $sum; //should display 6000
}

is this offset correct? $total_outstanding_amount['Arrears'] as i do not see it in the phpmyadmin table