【问题标题】:Add parameters in result array在结果数组中添加参数
【发布时间】:2018-12-30 09:22:19
【问题描述】:

我正在使用 Code Igniter,我尝试将 $finalamount 添加到 result_array。

   $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

【问题讨论】:

    标签: php mysql codeigniter model-view-controller


    【解决方案1】:

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

    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;
    }
    

    或使用传递引用(注意:&amp;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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-07
      • 2017-12-27
      • 1970-01-01
      • 2020-04-24
      • 2018-06-28
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多