MVC pattern for all developers


/ Published in: PHP
Save to your folder(s)

Useful for all ASP,JSP,PHP...and others to...follow the procedure


Copy this code and paste it in your HTML
  1. /******************************Model-View-Controller**************************/
  2.  
  3. /******************************Model*****************************************/
  4.  
  5. <?php
  6.  
  7. include("dbconfig.php"); //include this page fro defining the functions of the DB.
  8. class Model extends DbConfig
  9. {
  10. public function Model()
  11. {
  12. $this->defaultDBConfig();
  13. $this->set_connections();
  14. }
  15.  
  16. public function query($sql)
  17. {
  18. $result = mysql_query($sql);
  19. if($result)
  20. {
  21. return $result;
  22. }
  23. }
  24.  
  25. public function free_result($rs)
  26. {
  27. }
  28.  
  29. public function last_inserted_id()
  30. {
  31. return mysql_insert_id();
  32. }
  33.  
  34. }
  35. ?>
  36. /***************************End of Model*****************************************/
  37.  
  38. /***************************Controller***********************************************/
  39. <?php
  40.  
  41. include("model.php"); //include the model page
  42. class Employee extends Model
  43. {
  44. function get_records()
  45. {
  46. $result = $this->query("select * from emp");
  47. return $result;
  48. }
  49. }
  50. $o = new Employee;
  51. ?>
  52.  
  53. /*********************End of Controller***********************************************/
  54.  
  55.  
  56.  
  57. /*********************View***********************************************/
  58.  
  59. 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.
  60.  
  61. This process will not entirely make and application MVC but still considered to be the better and nearest way of making a application MVC
  62.  
  63. And for the rest of the languages u have to find a template engine... thats the best solution.....
  64.  
  65.  
  66. /*********************View***********************************************/
  67.  
  68.  
  69.  
  70. /*********************Connect to database***********************************************/
  71.  
  72. <?php
  73. abstract class DbConfig
  74. {
  75. private $dbHost;
  76. private $dbName;
  77. private $username;
  78. private $password;
  79.  
  80. public function defaultDBConfig()
  81. {
  82. $this->dbHost = "localhost";
  83. $this->dbName = "temp";
  84. $this->username = "root";
  85. $this->password = "";
  86. }
  87.  
  88. public function customDBConfig($dbHost, $username, $password, $dbName)
  89. {
  90. $this->dbHost = $dbHost;
  91. $this->dbName = $dbName;
  92. $this->username = $username;
  93. $this->password = $password;
  94. }
  95.  
  96. public function set_connections()
  97. {
  98. $conn = mysql_connect($this->dbHost, $this->username, $this->password) or die("Unable to connect with database");
  99. return mysql_select_db($this->dbName, $conn) or die("Database not found");
  100. }
  101. }
  102. ?>
  103.  
  104.  
  105. /*********************End of Connection***********************************************/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.