PHP MVC 開發

來源:互聯網
上載者:User
<?phpdb.class.phpclass db {protected $mysqli;protected $showError;public function __construct($configFile="password.inc.php",$showError=true){require_once($configFile);$this->mysqli=new Mysqli($host,$dbuser,$dbpassword,$dbname);if(mysqli_connect_error()){echo "l��ʧ��";exit();}}public function close(){$this->mysqli->close();}public function __destruct(){}}?>productModel.class.php<?php

require_once("db.class.php");

class ProductModel extends db {

  public function add($product)  {   $query= "insert into product values('',?,?,?)";   $stmt = $this->mysqli->prepare($query);

   $name = $product->getName();   $price =$product->getPrice();   $description = $product->getDescription();

     $stmt->bind_param("sss",$name,$price,$description);    $stmt->execute();   return $stmt->affected_rows==1?true:false;  }  public function delete($id)  {   $sql = "delete from product where id=?";   $stmt  =$this->mysqli->prepare($sql);

   $stmt->bind_param("i",$id);   $stmt->execute();   return $stmt->affected_rows==1? true:false;  }  public function modify($id,$product)  {   $sql = "update product set name=?,price=?,description=? where id=?";

   $stmt= $this->mysqli->prepare($sql);

   $name= $product->getName();   $price=$product->getPrice();   $description = $product->getDescription();   $stmt->bind_param("sssi",$name,$price,$description,$id);

   $stmt->execute();   return $stmt->affected_rows==1?true:false;  }  public function getOne($id)  {   $sql ="select * from product where id=".$id;   $result = $this->mysqli->query($sql);   $row= $result->fetch_assoc();   return $row;  }  public function getAll()  {   $sql = "select * from product";   $result = $this->mysqli->query($sql);   while($row=$result->fetch_assoc())   {    $all[] =$row;   }   return $all;  }}

?>product.php

<?php

class Product {

 private $id; private $name; private $price; private $description;

 public function __construct($name,$price,$des) {  $this->name=$name;  $this->price=$price;  $this->description=$des; } public function getId() {  if(!empty($this->id))   return $this->di;   else   return false; } public function getName() {  if(!empty($this->name))   return $this->name;   return "δ֪��Ʒ���"; } public function getPrice() {  if(!empty($this->price))   return $this->price;   else   return "δ֪�۸�";

 } public function getDescription() {  return $this->description; } public function __toString() {  return $this->id."---".$this->name."---".$this->price."---".$this->description."</br>"; } public function __destruct() {

 }}

$pro = new Product("apple","100","this is apple");

?>

Action.php

<?php

 

function __autoload($classname){ require_once ($classname).".class.php";} global $Model ;$Model = new ProductModel();$act = $_GET['act'];$id = $_GET['id'];

if(!empty($act) && !empty($id)){ switch($act) {  case "del": del($id); header("location:index.php"); break;  case "modify":    break; }}

function del($id){ global $Model; return $Model->delete($id);}

/* * *     簡單的MVC開發模式  * *   1.MVC 開發模式包括: Model(模型)  Action(控制器) View(視圖) *   2.Action主要負責控制流程程,指派任務 *   3.Model 主要負責做事的,Action指派的任務大多由它來完成,如果資料庫的增刪查改 *   4.View 主要是顯示前台,給使用者一個介面,可以由Smarty 來實現,不要自己寫編譯 * * *   Model 一般都是一些資料庫的操作,可寫成一個類,以包括了一所有的資料庫的操作 *   在Action裡面我們可以根據傳過來的值來進行相應的處理 *   如果我們對資料進行增刪查改,我們可以傳過來一個動作,根據這個動作來進行相應的處理, *   這幾天看 ecshop 源碼,裡面也都是用這種方法,不要將對每一個操作在定義一個頁面,那樣 *   會相當複雜 * *   <a href=Action.php?act=del&id=1>刪除</a> *   我們可以以這種形式來傳值,那麼在接收端,我們可以這麼做 *  $act = $_GET['act']; *  $id = $_GET['id']; *  if(!empty($act)&&!empty($id)) *  {    switch($act)    {     case "del" :delete($id);header("location:index.php");break;     case "modify": modify($id);break;    }   } * * * * */

 

 

?>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.