Reputation: 118
I am new to PHP and Codeigniter, and I am declaring a class level variable which I wanted to access in model class. I'm getting an error that the variable is not defined. Here is my code:
class Country_model extends CI_Model{
protected $table = 'COUNTRY';
function __construct()
{ // Call the Model constructor
parent::__construct();
}
function retriveAll(){
$q = $this->db->from($table)
->order_by('ID','ASC')
->get();
if ($q->num_rows()>0){
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
I have declared $table
and accessing in retriveAll
function. Please help me.
Upvotes: 2
Views: 3129
Reputation: 25445
That's not how you access class variables. Try using $this->table
instead:
function retriveAll(){
$q = $this->db->from($this->table)
->order_by('ID','ASC')
->get();
if ($q->num_rows()>0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Upvotes: 5