<?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; } } * * * * */
?>