我正在使用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
您只是从数据库返回结果数组,而不是实际将最终数量添加到各个行。你必须这样做:
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;
}
OR使用pass-by-reference(注意:&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;