Custom MVC: select count from db and display number of rows

Im Working for the first time with custom MVC and wanted to display number of rows fetched from DB. I created a Model (controles_model.php) using this code:

class Controles_Model extends Model{

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

    function controle_counter(){
        $query = $this->db->prepare("SELECT COUNT(*) FROM contrĂ´le");
        $query->execute();
        $count = $query->rowCount();
        return $count;
    }
}

To load the model I created this class to load the 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 to display the result this way:

<?php $this->controle_counter; ?>

But it doesn't work. here is the error message it gives me:

Notice: Undefined property: View::$controle_counter in D:\xampp\htdocs\exam_planning\assets\template\controles.php on line 13

Answers


in your controller you have to pass your model to the view like 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 this
    }
}
}

then in your view:

<?php echo (!empty $model) ? $model->controle_counter : ''; ?> 

also checks if the model is actually there, in case file doesn't exist.


Need Your Help

view website running on local machine via android

c# android web-services ipv4

I have a webservice in c# ,I want to access it via my android device through adhoc , I am able to get it in emulator using 10.0.2.2; but can't see on android device, Kindly suggest me any method to