完成封装MVC(四)后,新建控制器基类为controller.class.php,控制器的主要功能就是总调度,具体具体内容如下:
<?php
class Controller {
protected $_model;
protected $_controller;
protected $_action;
protected $_template;
function __construct($model, $controller,$action) {
$this->_controller = $controller;
$this->_action = $action;
$this->_model = $model;
$this->$model =new $model;
$this->_template =new Template($controller,$action);
}
function view($name='',$array=array()){
$this->_template->set($name,$array);
}
function __destruct() {
$this->_template->render();
}
}
如图所示:
新建控制器基类为model.class.php,考虑到模型需要对数据库进行处理,所以可以新建一个数据库基类sqlquery.class.php,模型去继承sqlquery.class.php。
新建sqlquery.class.php,代码如下:
<?php
class SQLQuery {
protected $_dbHandle;
protected $_result;
/** 连接数据库 **/
function connect($address, $account, $pwd, $name) {
$this->_dbHandle = @mysql_connect($address, $account, $pwd);
if ($this->_dbHandle != 0) {
if (mysql_select_db($name, $this->_dbHandle)) {
return 1;
}else {
return 0;
}
}else {
return 0;
}
}
/** 中断数据库连接 **/
function disconnect() {
if (@mysql_close($this->_dbHandle) != 0) {
return 1;
} else {
return 0;
}
}
/** 查询所有数据表内容 **/
function selectAll() {
$query = 'select * from `'.$this->_table.'`';
return $this->query($query);
}
/** 查询数据表指定列内容 **/
function select($id) {
$query = 'select * from `'.$this->_table.'` where `id` = \''.mysql_real_escape_string($id).'\'';
return $this->query($query, 1);
}
/** 自定义SQL查询语句 **/
function query($query, $singleResult = 0) {
$this->_result = mysql_query($query, $this->_dbHandle);
if (preg_match("/select/i",$query)) {
$result = array();
$table = array();
$field = array();
$tempResults = array();
$numOfFields = mysql_num_fields($this->_result);
for ($i = 0; $i < $numOfFields; ++$i) {
array_push($table,mysql_field_table($this->_result, $i));
array_push($field,mysql_field_name($this->_result, $i));
}
while ($row = mysql_fetch_row($this->_result)) {
for ($i = 0;$i < $numOfFields; ++$i) {
$table[$i] = trim(ucfirst($table[$i]),"s");
$tempResults[$table[$i]][$field[$i]] = $row[$i];
}
if ($singleResult == 1) {
mysql_free_result($this->_result);
return $tempResults;
}
array_push($result,$tempResults);
}
mysql_free_result($this->_result);
return($result);
}
}
/** 返回结果集行数 **/
function getNumRows() {
return mysql_num_rows($this->_result);
}
/** 释放结果集内存 **/
function freeResult() {
mysql_free_result($this->_result);
}
/** 返回MySQL操作错误信息 **/
function getError() {
return mysql_error($this->_dbHandle);
}
}
如图所示:
下面操作请点击:封装MVC(五)