mvc model

13
MVC 20/10/2011 [email protected]

Upload: nguyen-hien

Post on 29-Jun-2015

631 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Mvc Model

[email protected]

MVC

20/10/2011

Page 2: Mvc Model

[email protected]

MVC

• Bắt đầu từ những năm 1970. Lần đầu công bố vào 1978.

• MVC là viết tắt của Model – View – Controller

• Chia ứng dụng thành 3 thành phần–Model– View– Controller

Page 3: Mvc Model

[email protected]

Model

•Model giao tiếp với cơ sở dữ liệu.

•Model nhận và lưu trữ trong một cơ sở dữ liệu.

•Ví dụ, một đối tượng Product có thể nhận thông tin từ Database

•* Nếu thay đổi CSDL thì chỉ cần thay đổi Model

MY SQL-SQL-XML-Text

Page 4: Mvc Model

[email protected]

View•Hiển thị giao diện ứng dụng

•Giao diện người dùng được tạo ra từ dữ liệu trong các đối tượng Model

•Ví dụ, có thể chỉnh sửa phần hiển thị của một bảng Products

Page 5: Mvc Model

[email protected]

Controller

• Controller điều khiển sự tương

tác của người dùng. Xử lý

request từ url và form

• Làm việc với các đối tượng

• Chọn một đối tượng View để

hiển thị giao diện người dùng

Page 6: Mvc Model
Page 7: Mvc Model

[email protected]

Trình tự hoạt động (01)

Page 8: Mvc Model

[email protected]

Trình tự hoạt động (02)

Page 9: Mvc Model

[email protected]

Trình tự hoạt động (03)

Page 10: Mvc Model

[email protected]

Model.php•  <?php

 function dbconnect() {    static $connect=null;    if($connect==null){    $connect=mysql_connect("localhost","root","root");    mysql_select_db("autoshop");   }   return $connect;  } function closedb() {    mysql_close($con); } function getuser($usersId) {    $query=mysql_query("SELECT * FROM users WHERE usersId='$usersId'",dbconnect());    return mysql_fetch_assoc($query);

 }?>

Page 11: Mvc Model

[email protected]

View.php• <?php

echo $users["usersName"];?><form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>"><input type="text" name="usersId"/><input type="submit" value="Submit"/></form>

Page 12: Mvc Model

[email protected]

Control.php• <?php

 require_once("../mvc/model.php"); if($_SERVER["REQUEST_METHOD"]=="POST") {      header("HTTP/1.1 301 Moved Permanently");     header("location:".$_SERVER["PHP_SELF"]."?usersId=".$_POST["usersId"]);    exit; }else {

   $users=getuser($_GET["usersId"]);    require("../mvc/view.php"); }?>

Page 13: Mvc Model

[email protected]

Q&A