ccdavies
ccdavies

Reputation: 1606

CodeIgniter - unable to query database

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

        }
    }
}

Upvotes: 1

Views: 452

Answers (3)

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

From what I understand you, just want to increment upload_total.

Note: Don't use return $upload_total->result(); in between your function without putting a condition.

Upvotes: 1

mic
mic

Reputation: 1273

here is the solution

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

try that. it was becuase u was trying to use the DB returned object as an int.

Upvotes: 0

rcpayan
rcpayan

Reputation: 535

result of query is not an integer, it is an object

try like this;

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

btw, I'm not sure what you want to do, so i just corrected it!

Upvotes: 0

Related Questions