PHP MVC 开发

<?php
						db.class.php
class 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;     }    }  *  *  *  * */

 

 

?>

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值