Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

Access your class variable with $this, like:

$this->table
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top