在结果数组中添加参数

lnvxswe2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(318)

我正在使用代码点火器,我尝试向结果数组添加$finalamount。

$this->db->select('customer_id, customer_ref, i_points, group_points, created_on');

    $run_q = $this->db->get('employees');   

 if($run_q->num_rows() > 0){

        foreach ($run_q->result_array() as $get) {
            $totalpoints = $get['i_points'] + $get['group_points'];

            $percent = 17;

            $amountbycustomer = $totalpoints * 26 * $percent;

            $amountbypref = $this->final($get['customer_id'],$percent);

            $finalamount = $amountbycustomer + $amountbypref;

            $get['finalamount'] = $finalamount;
        }
        return $run_q->result_array();

    }

但在我看来,这个错误

<td class="employeeCustomer"><?=$get['customer_id']?></td>
<td class="employeeCustomerRef"><?=$get['customer_ref']?></td>    
<td class="employeeAmount"><?=$get['finalamount']?></td>//line 65

消息:未定义索引:finalamount
文件名:employees/employeeslist.php
行号:65

vngu2lb8

vngu2lb81#

您只是从db返回结果数组,而不是实际地将最终数量添加到各个行中。你必须这样做:

public function stmt() {
    $this->db->select('customer_id, customer_ref, i_points, group_points, created_on');
    $run_q = $this->db->get('employees');
    if ($run_q->num_rows() > 0) {
        $rows = $run_q->result_array();
        foreach ($rows as $k => $get) {
            $totalpoints = $get['i_points'] + $get['group_points'];
            $percent = 17;
            $amountbycustomer = $totalpoints * 26 * $percent;
            $amountbypref = $this->final($get['customer_id'], $percent);
            $finalamount = $amountbycustomer + $amountbypref;
            $rows[$k]['finalamount'] = $finalamount;
        }
        return $rows;
    }
    return false;
}

或使用参考传递(注意: &get ):

$rows = $run_q->result_array();
    foreach ($run_q->result_array() as &$get) {
        $totalpoints = $get['i_points'] + $get['group_points'];

        $percent = 17;

        $amountbycustomer = $totalpoints * 26 * $percent;

        $amountbypref = $this->final($get['customer_id'], $percent);

        $finalamount = $amountbycustomer + $amountbypref;

        $get['finalamount'] = $finalamount;
    }
    return $rows;

相关问题