1

I have written a function, and it's working except one part:

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

Can anyone tell me if they can see any issue with this particular part of the code?

Here is the full function. It runs in an extension. The extension is working, it just appears to be the above code that isnt.

I am basically just trying to find the value in the db, then if the value is more than one, add the value to another.

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

        }
    }
}
4

3 回答 3

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();在没有条件的情况下在函数之间使用。

于 2013-05-15T13:47:23.823 回答
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。

于 2013-05-15T13:41:52.803 回答
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;

顺便说一句,我不确定你想做什么,所以我只是纠正了它!

于 2013-05-15T13:46:12.833 回答