我正在尝试添加表格中的所有内容(交易),然后尝试将所有总计插入另一个表格(reports_daily),但没有插入任何内容。
transaction.php
public function index()
{
$this->load->model('transactions_model');
$transresult = $this->transactions_model->get_transactions_list();
$data['translist'] = $transresult;
$this->load->view('transactions_view',$data);
}
function create() {
$transresult = $this->transactions_model->get_transactions_list();
$data['translist'] = $transresult;
$sum=0;
$items=0;
$orders=0;
$date=$translist[0]->date_d;
for ($i=0; $i<count($translist); ++$i){
$sum += $translist[$i]->total_amount;
$items += $translist[$i]->no_items;
$orders++;
}
$data = array(
'date_d' => $date,
'total_items' => $items,
'total_orders' => $orders,
'total_sales' => $sum
);
//$this->db->set($data);
//$this->db->insert('reports_daily', $data);
$this->transactions_model->insert_reports($data);
$this->index();
}
transactions_model.php
function get_transactions_list()
{
$sql = 'select * from transactions order by date_d desc';
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
function insert_reports($data)
{
$this->db->insert('reports_daily', $data);
return;
}
帮助:(
答案 0 :(得分:0)
$translist
。因此,请使用$transresult[0]
代替$translist[0]
;
function create() {
$transresult = $this->transactions_model->get_transactions_list();
$sum=0;
$items=0;
$orders=0;
$date = $transresult[0]->date_d;
for ($i=0; $i<count($translist); ++$i){
$sum += $translist[$i]->total_amount;
$items += $translist[$i]->no_items;
$orders++;
}
$data = array(
'date_d' => $date,
'total_items' => $items,
'total_orders' => $orders,
'total_sales' => $sum
);
//$this->db->set($data);
//$this->db->insert('reports_daily', $data);
$this->transactions_model->insert_reports($data);
$this->index();
}