20181018 完整的mvc

目錄php

以及文件夾裏面詳細目錄css

back/controller裏面的    AdminController.class.php  代碼html

<?php
//後臺首頁控制器
 session_start();
class AdminController  extends  PlatFromController{
   
	function indexAction(){
		require VIEW_PATH."index.html";
	}
	function topAction(){
		require VIEW_PATH."top.html";
	}
	function menuAction(){
		require VIEW_PATH."menu.html";
	}
	function mainAction(){
		require VIEW_PATH."main.html";
	}
	function categoryListAction(){
		require VIEW_PATH."categoryList.html";
	}
	function goodslistAction(){
		//實例化模型類  返回數據
		$goodsModel=Factory::M("GoodsModel");
		//返回數據
		$list=$goodsModel->getList();
		require VIEW_PATH."goodslist.html";
	}
	//添加商品
	function goodsAddAction(){
		require VIEW_PATH."goodsAdd.html";
	}
	//刪除商品
	function  delAction(){
		$id=$_GET[id];
		//實例化商品模型
		$goodsModel=Factory::M("GoodsModel");
		//調用刪除方法
		$goodsModel->delData($id);
		//顯示頁面
		$list=$goodsModel->getList();
		require VIEW_PATH."goodslist.html";
		
	}
}

 back/controller裏面的 GoodsController.class.php  代碼mysql

<?php

//商品控制器
class  GoodsController  extends PlatFromController{
	//添加商品
	function insertAction(){
	//收集表單數據
	$data["goods_name"]=$_POST["goods_name"];
	$data["shop_price"]=$_POST["shop_price"];
	$data["goods_number"]=$_POST["goods_number"];
		
	  //上傳商品圖片原圖
		$t_upload = new Upload();
		$t_upload->upload_path = './web/upload/';
		$t_upload->prefix = 'goods_ori';
		if ($result = $t_upload->uploadOne($_FILES['goods_img'])) {
			//成功
			$data['goods_img'] = $result;
		} else {
			//上傳失敗
			$this->_jump('index.php?p=back&c=Admin&a=goodsAdd', '上傳失敗,<br>' . $t_upload->getError());
		}	
		
    
	//獲取模型對象
	$goodsModel=Factory::M("GoodsModel");
	//調用模型對象添加方法
		$goodsModel->addData($data);
	//顯示頁面
		$this->_jump("index.php?p=back&c=Admin&a=goodsAdd","添加成功");
		require VIEW_PATH."goodsAdd.html";
  }
}

 back/controller裏面的   LoginController.class.php  代碼web

<?php
//登陸的控制器
class LoginController extends PlatFromController{
	//登陸表單動做
	public  function  loginAction(){
		//載入當前的視圖層模板
		require VIEW_PATH."login.html";
	}
	//登陸驗證
	function checkAction(){
		$uid=$_POST["uid"];
		$pwd=$_POST["pwd"];
		$yzm=$_POST["yzm"];
		
		$Captcha=new Captcha();
		if(!$Captcha->checkCaptcha($yzm)){
			$this->_jump("index.php?p=back&c=Login&a=login","驗證碼錯誤");
		}
		
		//鏈接數據庫  把帳號密碼傳過去  驗證
		$userModel = Factory::M("UserModel");
		
		//根據返回結果  跳轉
		if($userModel->check($uid,$pwd)){
			//登陸成功
			session_start();
			$_SESSION["login"]="yes";
	       $this->_jump("index.php?p=back&c=Admin&a=index");
		}else{
			//登陸失敗
			 $this->_jump("index.php?p=back&c=Login&a=login","登陸失敗");
			
		}
		
	}
	//輸出驗證碼
	function  CaptchaAction(){
		$yzm=new Captcha();
		$yzm->generate();
	}
	//退出  動做
	function quitAction(){
		unset($_SESSION["login"]);
		$this->_jump("index.php?p=back&c=Login&a=login");
	}
	
}

  

 back/controller裏面的   PlatFromController.class.php  代碼 sql

<?php
//平臺控制器類  集中驗證
class PlatFromController extends Controller{
	//構造方法
	function __construct(){
		parent::__construct();
		$this->_checkLogin();
	}
	
	
	//驗證登陸操做
   function _checkLogin(){
	   session_start();
	   //不須要驗證的方法
	   $no_list=array(
		   "Login" =>array("login","check","Captcha")
	   );
	   //判斷不須要驗證
	   //控制器名=》當前控制器不須要驗證的動做名列表組
	   if(isset($no_list[CONTROLLER])&& in_array(ACTION,$no_list[CONTROLLER])){
		   return;
	   }
	   //須要驗證的
	   if(!isset($_SESSION["login"])){
		   $this->_jump("index.php?p=back&c=Login&a=login","非法登陸-請登陸",5);
	   }
   }
}

  back/model裏面的    GoodsModel.class.php  代碼數據庫

<?php

//商品列表模型
class  GoodsModel extends  Model{
	//獲取列表
	function getList(){
		$sql="select goods_id,goods_name,shop_price,goods_number  from p34_goods";
		return $this->dao->getAttr($sql);
	}
	//添加數據
	function addData($data){
		$sql="insert  into p34_goods(goods_name,shop_price,goods_number)values('".$data['goods_name']."','".$data['shop_price']."','".$data['$goods_number']."')";
		$this->dao->DbQuery($sql);
	}
	//刪除數據
	function delData($id){
		$sql="delete  from  p34_goods where goods_id='$id'";
		$this->dao->DbQuery($sql);
	}
	
}

  

  back/model裏面的   UsersModel.class.php  代碼json

<?php
//用戶表的模型類
class UserModel extends Model{
	//登陸驗證的方法
	function check($uid,$pwd){
		$sql="select  pwd from user where id='$uid'";
		$res=$this->dao->getStr($sql);
		if($pwd != "" && $res != "" && $res ==$pwd){
			return true;
		}else{
			return false;
		}
	}
}

view裏面就只弄(index.html   login.html )api

index.html   代碼數組

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ECSHOP 管理中心</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="Text/Javascript" language="JavaScript">

if (window.top != window) {
    window.top.location.href = document.location.href;
}
</script>

<frameset rows="76,*" framespacing="0" border="0">
    <frame src="index.php?p=back&c=Admin&a=top" id="header-frame" name="header-frame" frameborder="no" scrolling="no">
    <frameset cols="180,*" framespacing="0" border="0" id="frame-body">
        <frame src="index.php?p=back&c=Admin&a=menu" id="menu-frame" name="menu-frame" frameborder="no">
        <frame src="index.php?p=back&c=Admin&a=main" id="main-frame" name="main-frame" frameborder="no">
    </frameset>
</frameset><noframes></noframes>
</head>
<body>
</body>
</html>

 

  

 

login.html   代碼

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ECSHOP 管理中心</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="web/Styles/general.css" rel="stylesheet" type="text/css" />
<link href="web/Styles/main.css" rel="stylesheet" type="text/css" />
</head>
<body style="background: #278296;color:white">
<form method="post" action="index.php?p=back&c=Login&a=check">
    <table cellspacing="0" cellpadding="0" style="margin-top:100px" align="center">
        <tr>
            <td>
                <img src="web/Images/login.png" width="178" height="256" border="0" alt="ECSHOP" />
            </td>
            <td style="padding-left: 50px">
                <table>
                    <tr>
                        <td>管理員姓名:</td>
                        <td>
                            <input type="text" name="uid" />
                        </td>
                    </tr>
                    <tr>
                        <td>管理員密碼:</td>
                        <td>
                            <input type="password" name="pwd" />
                        </td>
                    </tr>
                    <tr>
                        <td>驗證碼:</td>
                        <td>
                            <input type="text" name="yzm" class="capital" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right">
                            <img src="index.php?p=back&c=Login&a=Captcha" onClick="this.src='index.php?p=back&c=Login&a=Captcha&'+Math.random()">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="checkbox" value="1" name="remember" id="remember" />
                            <label for="remember">請保存我此次的登陸信息。</label>
                        </td>
                    </tr>
                    <tr>
                        <td> </td>
                        <td>
                            <input type="submit" value="進入管理中心" class="button" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
  <input type="hidden" name="act" value="signin" />
</form>
</body>

 

  config裏面的  application.config.php  代碼  

<?php
return array(
	//數據庫配置
	 "db" =>array(
	    "host"=>"localhost",
    	"name"=>"root",
    	"pwd"=>"",
    	"DbName"=>"z_075mvc",
	    "charset"=>"utf8",
	    "post" =>3306
	  ),
	//默認平臺
	"app" =>array(
	   "plat"=>"back",

),
	//後臺默認控制器和方法
	"back" =>array(
       "controller"=>"Login",
	   "action"=>"login"
),
	//前臺默認控制器和方法
	"front" =>array(
       "controller"=>"Login",
	   "action"=>"login"
),

	//後臺默認控制器和方法
	"test" =>array(
	   "controller"=>"Match",
	   "action"=>"list"
)
	
);

  

 

test/controller裏面的  MatchController.class.php  代碼

<?php


//隊伍控制器
class MatchController extends Controller{
	//顯示比賽列表
	function listAction(){
		

    $MatchModel=Factory::M("MatchModel");
    $match_list=$MatchModel->getlist();


    require VIEW_PATH."match_list_v.html";
	}
	//修改比賽列表
	function updateAction(){
		
		echo "這是修改比賽的方法";
	}

}

  

test/controller裏面的  TeamController.class.php  代碼

 

<?php


//隊伍控制器
class TeamController extends Controller{
		//顯示隊伍列表
	function  listAction(){
		
		$TeamModel=Factory::M("TeamModel");
        $team_list=$TeamModel->getlist();
        
		require VIEW_PATH."myview.html";
	}
	//刪除隊伍
	function removeTeamAction(){
		
		$id=$_GET["id"];
		$TeamModel=Factory::M("TeamModel");
        $team_list=$TeamModel->del($id);
		$team_list=$TeamModel->getlist();
		
      require VIEW_PATH."myview.html";
	}
}

 

  

 

test/modelr裏面的  MatchModel.class.php  代碼

<?php

class MatchModel extends Model{
 function getlist(){
	 
    //得到比賽列表數據
      $sql = "select t1.t_name as t1_name, m.t1_score, m.t2_score, t2.t_name as t2_name, m.m_time from `match` as m left join `team` as t1 ON m.t1_id = t1.t_id  left join `team` as t2 ON m.t2_id=t2.t_id";

      return  $this->dao->getAssoc($sql);
		
	}
} 

test/modelr裏面的  TeamModel.class.php  代碼

<?php


class TeamModel extends Model{
	function getlist(){
		$sql="select *from team";
		return $this->dao->getAssoc($sql);
	}
	function del($id){
		$sql="delete  from team where t_id=$id";
		$this->dao->DbQuery($sql);
	}
}

  

view裏面的  match-list_v.html  代碼

<!-- 利用HTML代碼展現數據 -->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>比賽列表</title>
</head>
<body>

<table>
<tr>
<th>球隊一</th>
<th>比分</th>
<th>球隊二</th>
<th>時間</th>

</tr>
<?php foreach($match_list as $row) : ?>
	<tr>
	<td><?php echo $row['t1_name'];?></td>
	<td><?php echo $row['t1_score'];?>:<?php echo $row['t2_score'];?></td>
	<td><?php echo $row['t2_name'];?></td>
	<td><?php echo date('Y-m-d H:i', $row['m_time']);?></td>
	</tr>
<?php endForeach;?>
</table>	
</body>
</html>

  

view裏面的  myview.html  代碼

<!-- 利用HTML代碼展現數據 -->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>比賽列表</title>
</head>
<body>

<table>
<tr>
<th>t_id</th>
<th>t_name</th>
<th>操做</th>
</tr>
<?php foreach($team_list as $row) : ?>
	<tr>
	<td><?php echo $row['t_id'];?></td>
	<td><?php echo $row['t_name'];?></td>
	<td>
		<a href="index.php?p=test&c=Team&a=removeTeam&id=<?php echo $row['t_id'];?>">刪除</a>
	</td>
	</tr>
<?php endForeach;?>
</table>	
</body>
</html>

  

framework/tool裏面的  Captcha.class.php  代碼

<?php


/**
 * 驗證碼 工具
 */
class Captcha {

	/**
	 * 輸出生成的驗證碼輸出
	 *
	 * @param $code_len=4 碼值的長度
	 * @return void
	 */
	public function generate($code_len=4) {
		//生成碼值
		$chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';//全部可能字符
		$chars_len = strlen($chars);
		$code = '';//初始化碼值字符串
		for($i=1; $i<=$code_len;++$i) {
			$rand_index = mt_rand(0, $chars_len-1);
			$code .= $chars[$rand_index];//字符串支持[]操做,經過下標取得某個字符
		}
		//echo $code;

		//存儲於session,用於驗證
		@session_start();//保證session機制必定是開啓的,同時重複開啓不會報錯,@屏蔽錯誤。
		$_SESSION['captcha_code'] = $code;

		//生成驗證碼圖片

		//背景圖
		$bg_file = TOOL_PATH . 'captcha/captcha_bg' . mt_rand(1, 5) . '.jpg';

		//基於jpg格式的圖片建立畫布
		$img = imageCreateFromJPEG($bg_file);

		//隨機分配字符串顏色
		$str_color = mt_rand(1, 3) == 1 ? imageColorAllocate($img, 0, 0, 0) : imageColorAllocate($img, 0xff, 0xff, 0xff);

		//字符串
		$font = 5;
		// 畫布尺寸
		$img_w = imageSX($img);
		$img_h = imageSY($img);
		// 字體的尺寸
		$font_w = imageFontWidth($font);
		$font_h = imageFontHeight($font);
		// 字符串的尺寸
		$code_w = $font_w * $code_len;
		$code_h = $font_h;
		$x = ($img_w-$code_w)/2;
		$y = ($img_h-$code_h)/2;
		imageString($img, $font, $x, $y, $code, $str_color);

		//輸出
		header('Content-Type: image/jpeg;');
		imageJPEG($img);

		//
		imageDestroy($img);
	}

	/**
	 * 驗證
	 * @param $request_code 用戶表單中提交的碼值
	 * @return bool 是否匹配
	 */
	public function checkCaptcha($request_code) {
		@session_start();
		//嚴格點,存在且相等(不區分大小寫)。
		//strCaseCmp()不區分大小寫字符串比較,返回值負,第一個小,返回正,第一個大,返回0,相等。strCmp也是相似,不過是區分大小寫比較。
		$result = isset($request_code) && isset($_SESSION['captcha_code']) && (strCaseCmp($request_code, $_SESSION['captcha_code'])==0);

		//安全考慮,銷燬session中的驗證碼值
		unset($_SESSION['captcha_code']);

		return $result;

	}
}

  

framework/tool裏面的  Upload.class.php  代碼

<?php


class Upload {
	private $_max_size;
	private $_type_map;
	private $_allow_ext_list;
	private $_allow_mime_list;
	private $_upload_path;
	private $_prefix;

	private $_error;//當前的錯誤信息
	public function getError() {
		return $this->_error;
	}

	public function __construct() {
		$this->_max_size = 1024*1024;
		$this->_type_map = array(
			'.png' 	=> array('image/png', 'image/x-png'),
			'.jpg'	=> array('image/jpeg', 'image/pjpeg'),
			'.jpeg'	=> array('image/jpeg', 'image/pjpeg'),
			'.gif'	=> array('image/gif'),
			);
		$this->_allow_ext_list = array('.png', '.jpg');
		$allow_mime_list = array();
		foreach($this->_allow_ext_list as $value) {
			//獲得每一個後綴名
			$allow_mime_list = array_merge($allow_mime_list, $this->_type_map[$value]);
		}
		// 去重
		$this->_allow_mime_list = array_unique($allow_mime_list);
		$this->_upload_path = './';
		$this->_prefix = '';
	}

	public function __set($p, $v) {//屬性重載
		$allow_set_list = array('_upload_path', '_prefix', '_allow_ext_list', '_max_size');
		//能夠不加:_ 進行設置
		if(substr($p, 0, 1) !== '_') {
			$p = '_' . $p;
		}

		$this->$p = $v;
	}


	/**
	 * 上傳單個文件
	 */
	public function uploadOne($tmp_file) {

		# 是否存在錯誤
		if($tmp_file['error'] != 0) {
			$this->_error = '文件上傳錯誤';
			return false;
		}

		# 尺寸
		if ($tmp_file['size'] > $this->_max_size) {
			$this->_error = '文件過大';
			return false;
		}

		# 類型
		# 保證修改容許的後綴名,就能夠影響到$allow_ext_list和$allow_mime_list
		# 設置一個後綴名與mime的映射關係
		
		# 後綴,從原始文件名中提取
		$ext = strtolower(strrchr($tmp_file['name'], '.'));
		if (!in_array($ext, $this->_allow_ext_list)) {
			$this->_error = '類型不合法';
			return false;
		}
		# MIME, type元素。
		// $allow_mime_list = array('image/png', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png');
		
		if (!in_array($tmp_file['type'], $this->_allow_mime_list)) {
			$this->_error = '類型不合法';
			return false;
		}
		//PHP本身獲取文件的mime,進行檢測
		$finfo = new Finfo(FILEINFO_MIME_TYPE);//得到一個能夠檢測文件MIME類型信息的對象
		$mime_type = $finfo->file($tmp_file['tmp_name']);//檢測
		if (!in_array($mime_type, $this->_allow_mime_list)) {
			$this->_error = '類型不合法';
			return false;
		}


		# 移動
		# 上傳文件存儲地址
		//建立子目錄
		$subdir = date('YmdH') . '/';
		if(!is_dir($this->_upload_path . $subdir)) {//檢測是否存在
			//不存在
			mkdir($this->_upload_path . $subdir);
		}
		# 科學起名
		$upload_filename = uniqID($this->_prefix, true) . $ext;
		if (move_uploaded_file($tmp_file['tmp_name'], $this->_upload_path . $subdir . $upload_filename)) {
			// 移動成功,返回文件名
			return $subdir . $upload_filename;
		} else {
			// 移動失敗
			$this->_error = '移動失敗';
			return false;
		}

	}
}

  

frameworkl裏面的  Controller.class.php  代碼

<?php
//基礎控制器
class Controller{
	function __construct(){
		$this->_initHead();
	}
	//設置字符集
	function _initHead(){
		header('Content-Type: text/html; charset=utf-8');
	}
	//跳轉
//@param   $url  路徑
//@param   $info 提示信息
//@param   $wait  等待時間
//return  void
	function  _jump($url,$info=null,$wait=3){
		if(is_null($info)){
			//當即
			header("location:".$url);
		}else{
			//提示後
			header("Refresh:$wait;URL=$url");
			echo $info;
		}
		//終止
		die;
	}
}

  

frameworkl裏面的  Factory.class.php  代碼

<?php
//單例工廠類
class Factory{
	//返回指定類的對象
	public  static  function M($model_name){
		//定義一個數組  用來各類對象
		static $model_list=array();
		
		//判斷數組中有沒有指定的對象
		if(!isset($model_list[$model_name])){
		   
			$model_list[$model_name]=new $model_name;
		}
		//返回對象
		return $model_list[$model_name];
	}
}

  

frameworkl裏面的   Framework.class.php  代碼   (很重要   框架類)

<?php
//框架初始化功能類
class Framework{
	//入口
 public  static  function  run(){
		
		//聲明路徑常量
		static::_initPath();
	    //獲取配置信息
	    static::_initConfig();
		//肯定分發參數
		static::_initParam();
		//當前平臺相關的路徑常量
		static::_initPlatPath();
		//註冊自動加載
		static::_initAutoload();
		//請求分發
		static::_dispatch();
	}
	//聲明路徑常量
	static function _initPath(){
		
        //目錄常量
        define("ROOT_PATH",getcwd()."/");
        
        define("APP_PATH",ROOT_PATH."application/");
        
        define("FRAME_PATH",ROOT_PATH."framework/");
		
        define("CONFIG_PATH",APP_PATH."config/");
		
        define("TOOL_PATH",FRAME_PATH."tool/");
	}
	//獲取配置信息
	static function _initConfig(){
		$GLOBALS["config"]=require CONFIG_PATH."application.config.php";
   
	}
	//肯定分發參數
	static function _initParam(){
    
        //參數分發  平臺
       
        //定義平臺常量
        define("PLATFORM",isset($_GET["p"])?$_GET["p"]:$GLOBALS["config"]["app"]["plat"]);
        //參數分發   控制器
     
        //定義控制器常量
        define("CONTROLLER",isset($_GET["c"])?$_GET["c"]:$GLOBALS["config"][PLATFORM]["controller"] );
        //參數分發   動做
      
        //定義動做常量
        define("ACTION",isset($_GET["a"])?$_GET["a"]:$GLOBALS["config"][PLATFORM]["action"] );
	}
	//當前平臺相關的路徑常量
	static function _initPlatPath(){
		
      
        //當前平臺相關常量
        define("CONTROLLER_PATH",APP_PATH.PLATFORM."/controller/");
        
        define("MODEL_PATH",APP_PATH.PLATFORM."/model/");
        
        define("VIEW_PATH",APP_PATH.PLATFORM."/view/");

	}
	//註冊自動加載
	static function _initAutoload(){
	
      //註冊自動加載方法
      spl_autoload_register(array(__CLASS__,"userAutoload"));
	}
	
    //自動加載的方法  自動載入已知類文件
     static function  userAutoload($class_name){
    	
    	//先肯定肯定的(核心類)
    	//類名與類文件映射數組
    	$framework_class_list=array(
    	//類名=>類文件地址
    		"Controller" =>  FRAME_PATH."Controller.class.php",
    		"Model"      =>  FRAME_PATH."Model.class.php",
    		"Factory"    =>  FRAME_PATH."Factory.class.php",
    		"mysqlDb"    =>  FRAME_PATH."mysqlDb.class.php",
    		"Captcha"    =>  TOOL_PATH."Captcha.class.php",
    		"Upload"    =>  TOOL_PATH."Upload.class.php"
    	
    	);
    	//加載類
    	if(isset($framework_class_list[$class_name])){
    		//是核心類
    		require $framework_class_list[$class_name];
    	}else if(substr($class_name,-10)=="Controller"){
    		require CONTROLLER_PATH.$class_name.".class.php";
        }else if(substr($class_name,-5)=="Model"){
    		require MODEL_PATH.$class_name.".class.php";
    	}
    }
   //請求分發
	static function _dispatch(){
         $controllerName=CONTROLLER."Controller";//組織控制器名
		
         $Action=ACTION."Action";//組織方法名
		
         $controller=new $controllerName();
         $controller->$Action();
	}
	
	
}

  

frameworkl裏面的  Model.class.php  代碼

<?php
//基礎模型類
class Model{
	public $dao;//表明mysqlDb類對象
	//實例化模型對象時自動建立  mysqlDb對象
	function __construct(){
	 $this->_initDao();
	}
	//初始化數據庫操做類的對象
function _initDao(){
	
       $config=$GLOBALS["config"]["db"];
       $this->dao= mysqlDb::getInstance($config);
   }

}

	

  

frameworkl裏面的  mysqDb.class.php  代碼(用類封裝一個鏈接數據庫的方法)

<?php
class mysqlDb{
	public $host; //服務器地址
	public $post=3306; //端口
	public $name; //用戶名
	public $pwd; //m密碼
	public $DbName; //數據庫名稱
	public $charset;//默認編碼
	
	public $link;//數據庫鏈接對象
	public $res; //資源   返回結果集
	
	//三私一公
	private static $obj;//用來存newdb對象
	//構造方法 私有的
	private function __construct($config=array()){
		/*初始化參數*/
		$this->host = $config["host"] ? $config["host"] : "localhost";
		$this->name = $config["name"] ? $config["name"] : "root";	
		$this->pwd = $config["pwd"] ? $config["pwd"]  : "";
		$this->DbName = $config["DbName"] ?  $config["DbName"] : "mysql";
		$this->charset = $config["charset"] ?$config["charset"] : "utf8" ;
		
		/*鏈接數據庫  設置字符集*/
		$this->connectMysql();
		$this->setCharSet();
	}
	//私有化clone方法
	private  function __clone(){}
	//提供公共的返回對象方法
	static function getInstance($config=array()){
		if(!isset(self::$obj)){
			self::$obj=new self($config);
		}
		return self::$obj;
	}
	
	
	/*鏈接數據庫*/
	function connectMysql(){
		$this->link = new MySQLi($this->host,$this->name,$this->pwd,$this->DbName);
		!mysqli_connect_error() or die("鏈接數據庫失敗");
	}
	
	/*設置字符集*/
	function setCharSet(){
		$this->link->query("set names ".$this->charset );
	}
	
	/*執行sql語句的方法*/
	function DbQuery($sql){
		$this->res = $this->link->query($sql);
		if(!$this->res){
			echo ("<br />執行失敗。");
			echo "<br />失敗的sql語句爲:" . $sql;
			echo "<br />出錯信息爲:" . mysqli_error($this->link);
			echo "<br />錯誤代號爲:" . mysqli_errno($this->link);
			die;
		}
		return $this->res;
	}
   //返回字符串
	function getStr($sql){
		$zhi = $this->DbQuery($sql);
		//將結果集轉爲字符串
		$arr = $zhi->fetch_all();
		$brr = array();
		foreach($arr as $v){
			$brr[] = implode(",",$v);
		}
		return implode("^",$brr);
	}

	//返回json
	function getJson($sql){
		$zhi = $this->DbQuery($sql);
		$brr = array();
		while($row = $zhi->fetch_assoc()){
			$brr[] = $row;
		}
		return json_encode($brr);
	}
	/*返回關聯數組*/
	function getAssoc($sql){
		$zhi = $this->DbQuery($sql);
		$brr = array();
		while($row = $zhi->fetch_assoc()){
			$brr[] = $row;
		}
		return $brr;
	}
	//返回索引數組
	function getAttr($sql){
		$zhi = $this->DbQuery($sql);
		return $zhi->fetch_all();
	}

}

  

web裏面的照片  js  styles   upload(文件上傳照片儲存文件夾)

index.php  (很是重要的   入口文件)

<?php

require "./framework/Framework.class.php";

Framework::run();

 

 

更加詳細的代碼   在D:\wampserver\wamp\www\20181012   wanzheng MVC 

相關文章
相關標籤/搜索