封装MVC(四)

        完成封装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(五)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值