我已经编写了一个函数,除了一部分之外它正在工作:
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
return $upload_total->result();
// If upload total is more than 0
if($upload_total > 0) {
$new_total = $upload_total + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
任何人都可以告诉我他们是否可以看到代码的这一特定部分有任何问题吗?
这是完整的功能。它在一个扩展中运行。扩展正在运行,它似乎是上面的代码不是。
我基本上只是想在db中找到值,然后如果值大于1,则将值添加到另一个。
function cartthrob_on_authorize()
{
foreach ($this->EE->cartthrob->cart->items() as $item) {
// Create array to store member id and purchased upload slots
$my_data = array(
'member_id' => $this->EE->session->userdata('member_id'),
'upload_total' => $item->item_options('fees'),
);
// Store member id in variable
$member_id = $my_data['member_id'];
// Query table to check if there is record with member id exists
$query = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If query returns more than 0 update record
if($query->num_rows() > 0) {
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If upload total is more than 0
if($upload_total > 0) {
$new_total = $upload_total + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
return $upload_total->result();
$this->EE->db->where('member_id', $member_id);
$this->EE->db->update('exp_competition_purchase_upload_total', $my_data);
// If query returns 0 insert new record
} else {
$this->EE->db->insert('exp_competition_purchase_upload_total', $my_data);
}
}
}
答案 0 :(得分:1)
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id))->result_array();
$upload_total = $upload_total[0];
//If upload total is more than 0
$upload_total_col = $upload_total['upload_total']>0 ? $upload_total['upload_total']:0;
$my_data['upload_total'] += $upload_total_col;
根据我的理解,您只想增加upload_total。
注意:在不添加条件的情况下,请勿在您的函数之间使用return $upload_total->result();
。
答案 1 :(得分:0)
这是解决方案
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If upload total is more than 0
if($upload_total->num_rows() > 0) {
$result = $upload_total->row_array();
$new_total = $result['upload_total'] + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
return $my_data;
试试看。这是因为我试图将DB返回的对象用作int。
答案 2 :(得分:0)
查询结果不是整数,它是一个对象
尝试这样;
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
$result = $upload_total->result();
// If upload total is more than 0
if($upload_total->num_rows() > 0) {
$my_data['upload_total'] += $result->upload_total;
}
return $my_data;
顺便说一句,我不确定你想做什么,所以我纠正了它!