Pergunta

I have a class in Codeigniter which extends CI_Model.

I have declared a global array $data, but I can't access this array from my functions which i want to push some items. that shows

A PHP Error was encountered Message: Undefined variable: data

and Fatal error: Cannot access empty property in /api/system/core/Model.php on line 51

code for class:

class Subscription_collection_model extends CI_Model {
        public $data=array();
    /*
    |-----------------------------------------
    | Constructor
    |-----------------------------------------
    */

    function __construct() {
        parent::__construct();
            }

    function get() {

            $this->db->select('family.house_name,family.id,family_grade.amount');
            $this->db->from('family');
            $this->db->join('family_grade','family_grade.id = family.family_grade_id');
            $query = $this->db->get();
             if ($query->num_rows() > 0)
               {
                foreach ($query->result() as $row)
                    {
                        $this->findBalance($row->id);
                     }
                }    
         return $data;
        }


     function findBalance($id) {

        $this->db->where('family_id', $id);
        $this->db->select_sum('collecting_amount');
        $query = $this->db->get('subscription_collection');

           if ($query->num_rows() > 0)
               {
                foreach ($query->result() as $row)
                    {
                        array_push($this->$data,$row->collecting_amount);


                     }
                }
    }
}
Foi útil?

Solução

you got a typo there. you want to call $this->data not $this->$data. the latter would assume, you wanna call a dynamic variable on the instance.

$prop = 'myProperty';
$this->$prop = 'test';

is the same as

$this->myProperty = 'test';

Outras dicas

How about:

return $this->data;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top