im working first time custom mvc , wanted display number of rows fetched db. created model (controles_model.php) using code:
class controles_model extends model{ public function __construct(){ parent::__construct(); } function controle_counter(){ $query = $this->db->prepare("select count(*) contrĂ´le"); $query->execute(); $count = $query->rowcount(); return $count; } }
to load model created class load model dynamically.
class controller{ function __construct(){ $this->view = new view(); } public function loadmodel($name){ $path = 'models/' . $name . '_model.php'; if(file_exists($path)){ require 'models/' . $name . '_model.php'; $modelname = $name . '_model'; $this->model = new $modelname(); } } }
i tried display result way:
<?php $this->controle_counter; ?>
but doesn't work. here error message gives me:
notice: undefined property: view::$controle_counter in d:\xampp\htdocs\exam_planning\assets\template\controles.php on line 13
in controller have pass model view this:
class controller{ function __construct(){ $this->view = new view(); } public function loadmodel($name){ $path = 'models/' . $name . '_model.php'; if(file_exists($path)){ require 'models/' . $name . '_model.php'; $modelname = $name . '_model'; $this->model = new $modelname(); $this->view->model = $this->model; // <--- add } } }
then in view:
<?php echo (!empty $model) ? $model->controle_counter : ''; ?>
also checks if model there, in case file doesn't exist.
Comments
Post a Comment