/ Published in: PHP
Useful for all ASP,JSP,PHP...and others to...follow the procedure
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/******************************Model-View-Controller**************************/ /******************************Model*****************************************/ <?php include("dbconfig.php"); //include this page fro defining the functions of the DB. class Model extends DbConfig { public function Model() { $this->defaultDBConfig(); $this->set_connections(); } public function query($sql) { if($result) { return $result; } } public function free_result($rs) { } public function last_inserted_id() { } } ?> /***************************End of Model*****************************************/ /***************************Controller***********************************************/ <?php include("model.php"); //include the model page class Employee extends Model { function get_records() { $result = $this->query("select * from emp"); return $result; } } $o = new Employee; ?> /*********************End of Controller***********************************************/ /*********************View***********************************************/ Note: Here u can use what ever u want but write the HTML, ASP, JSP without the variables, messages or what ever which u write in the business logic layer has to be filtered there itself.....and for this template engine is the best solution ex - Smarty which allows u to write and assign the variables at controller page itself to and those can be called printed on the html page by using smarty variables and functions. This process will not entirely make and application MVC but still considered to be the better and nearest way of making a application MVC And for the rest of the languages u have to find a template engine... thats the best solution..... /*********************View***********************************************/ /*********************Connect to database***********************************************/ <?php abstract class DbConfig { private $dbHost; private $dbName; private $username; private $password; public function defaultDBConfig() { $this->dbHost = "localhost"; $this->dbName = "temp"; $this->username = "root"; $this->password = ""; } public function customDBConfig($dbHost, $username, $password, $dbName) { $this->dbHost = $dbHost; $this->dbName = $dbName; $this->username = $username; $this->password = $password; } public function set_connections() { $conn = mysql_connect($this->dbHost, $this->username, $this->password) or die("Unable to connect with database"); } } ?> /*********************End of Connection***********************************************/