How can I get the variable in the model for the controller? How can I get the variable in the model for the controller? codeigniter codeigniter

How can I get the variable in the model for the controller?


You can declare a private variable, say send in your model and make getter and setter in your model class in an encapsulated way to get the value in the controller, like below:

Snippet:

Model:

<?phpclass Yourmodel extends CI_Model{    private $send;    function __construct() {        parent::__construct();        $this->send = [];    }    public function register_user($send){        if($this->emailVerify()) {          $this->send = array(            'tipo' => 'Error.'          );          return true;        }         return false;    }    public function setSendValue($value){        $this->send = $value;    }    public function getSendValue(){        return $this->send;    }}

Controller:

<?phpclass Controller extends CI_Controller{    private $send;    public function __construct(){        parent::__construct();        $this->send = [];    }    public function register(){        $this->load->model('users');        if($this->users->register_user()){            $this->send = $this->users->getSendValue();        }        $this->load->view('signup', $this->send);    }}


Replace your code modal and controller as follows:

  1. You need not to declare $send in modal definition, as you are not passing any value while calling the same modal function.
  2. modal positive return can be array $send itself
  3. Catch value of modal function

Modal :

public function register_user()  {    if($this->emailVerify()) {      $send = array(        'tipo' => 'Error.'      );      return $send;    } else {      return false;    }

Controller:

       public function __construct()        {            parent::__construct();            $this->send;        }        public function register()            {                $this->load->model('users');                $send = $this->users->register_user();        //print_r($send); // You will get data here                $this->load->view('signup', $this->send);            }


Model

public function register_user($send = "")  {    if($this->emailVerify()) {      $send = array(        'tipo' => 'Error.'      );      return $send;    } else {      return false;    }

Controller

public function __construct()    {        parent::__construct();        $this->send;    }    public function register()        {            $this->load->model('users');            $sendRes = $this->users->register_user(); //now you can use this $send response variable            $this->load->view('signup', $this->send);        }