shavindip
shavindip

Reputation: 622

CodeIgniter : Undefined index in model variable

I have the below code in of of my models. Im loading the offer details page and it gave me the error

Severity: Notice

Message: Undefined index: viewc

Filename: statistics/Statistic_model.php

Line Number: 551

Statistic_model:

public function getTotalDetailsViewOfActiveOffer($comid) {

    $this->db->select('count(statistic_offer_view_count.id) as viewc');
    $this->db->from('statistic_offer_view_count');
    $this->db->join('offers', 'offers.id = statistic_offer_view_count.offer_id');       
    $this->db->where('offers.com_id',$comid);
    $this->db->where('offers.approved_status',"2"); 
    $this->db->where('offers.enddate >=',date('Y-m-d'));
    $this->db->group_by("offers.com_id");       
    $query = $this->db->get();
    $row = $query->result_array();
    $count=$row['viewc'];           //this is line 551

}

Any idea how to fix this error?

Upvotes: 1

Views: 219

Answers (2)

atta afridi
atta afridi

Reputation: 96

In Your Code You Selecting data from database Multiple array's.And assigning at to a variable like a single array. Your Code....

$row = $query->result_array();
$count=$row['viewc'];

correct way is in your conduction to assign a variable is....

$data   =   array();
foreah($row as $key => $value){
  $data[$key] = $value['viewc'];
}

Upvotes: 1

Alex
Alex

Reputation: 9265

That error means that the index viewc is not in the array $query->result_array() most likely because your query failed or returned null. You should get into the habit of doing:

$query = $this->db->get();
if ($query->num_rows() > 0) {
    return $query->row()->viewc;
}
return false; // or in this case 0 or whatever

Upvotes: 2

Related Questions