php MVC

What Exactly is a Model-View-Controller?

A Model-View-Controller is a pattern of software architecture. It controls how different applications interact with a user.
Here’s a fun fact: you’re looking at a MVC right now.
This web page is a perfect example of the MVC structure. Here, the HTML structure of this web page is the model, the CSS stylesheets that control its appearance are the ‘View’, and the browser that you use to interact with the web page is the ‘controller’.
Let’s look at this in a little more detail:
Model: The model is a representation of knowledge. That is, the model contains the actual data that has to be shown to the user. All data is held in a structured format. For example, the HTML on this web page holds the actual text that you see inside structured HTML <tags>.
View: The view is a visual representation of the model. The view informs the way data structured in the model will be made visible to the user. For example, the HTML may hold the text, but the colors, font size and font style information is actually held by independent CSS stylesheets – i.e. the view.
Controller: A controller is the environment that bridges the divide between the user and the system. This is the device that lets the user interact with the system. In the above examples, the browser is the controller that helps you interact with the web page.
One way of representing this is as follows:

Example:
we start with defining the index
index.php:
<?php
if  ($_GET['do']=="")
{                  
  require ("default.html");
}
else
{
  if($_GET['do']=="display")
  {
    require("action/controller.php");
    new Example();
  }
}
?>

in the index we define how to display a page using "do" : static page("default.html") or redirect to the controller  "controller.php".
Controller : /action/controller.php:
<?php
 require ("dao/model.php");

  class Controller
 {
  public function cc()
  {
    $exampleDao= new ExampleDao();
    $data = $exampleDao->get_products();
    require("web/display.phtml");

  }
}
?>


Model : model.php

<?php
 require ("class_connect_bd.php");

 class Model
 {
   public  function get_products()
   {
    $sql = "SELECT product.name, product.price FROM product where product.type=42";
    $query = mysql_query($sql) or die('Erreur SQL !<br>'.mysql_error());
    $all =  mysql_fetch_all($query);
    return $all;
   }
 }
?>

in the Model we define a function that return a product proprties from the database.

class_connect_bd.php:

<?php
$db = mysql_connect('localhost', 'user', 'password');
mysql_select_db('test',$db);

 function mysql_fetch_all($query, $kind = 'assoc') {
    $result = array();
    $kind = $kind === 'assoc' ? $kind : 'row';
    eval('while(@$r = mysql_fetch_'.$kind.'($query)) array_push($result, $r);');
    return $result;
}
?>





View: /web/display.phtml:


<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
  <head>
    <title>
      Example
    </title>
  </head>
  <body>
    <?
foreach ($data AS $row )
{
?>
   Nom :
<? echo $row["name"]; ?>
 Prix :
<? echo $row["price"]; ?> <br/>
<?
}
?>
  </body>
</html>


       Note:                                                                                                


   .PHP
 : Page doesn't contain view-related code

  .PHTML
 : Page contains little (if any) data logic and the most part of it is    

   presentation-related









Post a Comment

CodeNirvana
Newer Posts Older Posts
Copyright © JEsmairi
Back To Top