C语言编写的PHP框架--yaf入门编程

首先--添加dll,修改php.ini--不同的版本,不同的需求

其次,根据教程http://www.laruence.com/manual/tutorial.firstpage.html#tutorial.directory手动搭建好目录结构

入口文件index.php位置稍作修改--个人习惯

入口文件内容

<?php
header("Content-Type: text/html;charset=utf-8");
define("APP_PATH",  realpath(dirname(__FILE__) . '/../')); /* 指向public的上一级 */
$app  = new Yaf_Application(APP_PATH . "/yaf/conf/application.ini");
$app->run();

配置文件

配置文件内容

[product]
;支持直接写PHP中的已定义常量
application.directory=APP_PATH "/yaf/application" 

集成了一个数据库操作类

控制器写法

new DBModel();的意思是去models文件夹下找DB.php里的DBModel类

DB.php

D:WWWyafapplicationmodelsMysqli.php

<?php
/**
 * +----------------------------------------------------------------------
 * | WeizePHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2013 - 2113 http://75hh.com/weizephp/ All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 韦泽 <e-mail:weizesw@gmail.com> <QQ:310472156>
 * +----------------------------------------------------------------------
 * | 文件功能:mysqli 数据库操作、分页类
 * +----------------------------------------------------------------------
 */



/*

// 使用方法
// define('ROOT_PATH', '..'); // 单独提取本站的 db_mysqli 类在其他地方使用时,请注意错误日志 ROOT_PATH 路径是否正确

$host = 'localhost';
$username = 'root';
$passwd = '';
$dbname = 'weizephp';
$port = 3306;
$socket = NULL;
$dbcharset = 'utf8';
$error_reporting = TRUE; // $error_reporting 为 FALSE 时,显示错误,适用于开发的时候调试

$DB = new db_mysqli($host, $username, $passwd, $dbname, $port, $socket, $dbcharset, $error_reporting); // 连接数据库

echo '<br>---------------------------- 获取单条结果 ----------------------------<br><br>';

$sql = "SELECT * FROM `weize_user_admin_log`";
$row = $DB->get_one($sql); // 获取单条结果
print_r($row);

echo '<br><br>---------------------------- 获取全部结果 ----------------------------<br><br>';

$sql = "SELECT * FROM `weize_user_admin_log`";
$all = $DB->get_all($sql); // 获取全部结果
print_r($all);

echo '<br><br>---------------------------- 获取[分页]结果 ----------------------------<br><br>';

$sql = "SELECT * FROM `weize_user_admin_log`";
$page_data = $DB->page($sql); // 获取分页结果
$showpage = $DB->showpage();  // 获取上一页、下一页的变量
print_r($page_data);          // 输出分页数据
echo '<br><br>';
echo $showpage;               // 输出上一页、下一页的HTML字符串

*/



class MysqliModel extends mysqli {
    
    

    

    public $error_reporting = FALSE; // 是否显示错误报告,false为显示,true为不显示
    public $result = NULL; // 结果集
    
    public $page_count = 0; // 总记录数,用于分页
    public $page_all = 0; // 总页数
    public $page_current = 1; // 当前页
    public $page_size = 10; // 每页显示条数
    
    /**
     * 构造函数
     */
    public function __construct($host, $username, $passwd, $dbname = '', $port = 3306, $socket = NULL, $dbcharset = 'utf8', $error_reporting = TRUE) {
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        if($this->connect_error) {
            if($error_reporting === FALSE) {
                die('Could not connect: (' . $this->connect_errno . ') '. $this->connect_error);
            } else {
                // 记录连接错误日志
                $url = htmlspecialchars($_SERVER['REQUEST_URI']);
                $time = time();
                $date = date('Y-m-d H:i:s', $time);
                $data = "<?php exit; ?> [DATE]$date [URL]$url [CONNECT_ERRNO]". $this->connect_errno ." [CONNECT_ERROR]". $this->connect_error ."
";
                error_log($data, 3, ROOT_PATH . '/data/log/'.date('Ymd', $time).'_mysql_error.php');
                // 停止程序执行
                //die('Could not connect MySQL.');
            }
        }
        $this->error_reporting = $error_reporting;
        $this->set_charset($dbcharset);
    }
    
    /**
     * 数据库执行语句,可执行查询添加修改删除等任何sql语句
     * 失败时返回 FALSE,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回TRUE。
     */
    public function query($sql, $resultmode = MYSQLI_STORE_RESULT) {
        $this->result = parent::query($sql, $resultmode);
        if($this->result === FALSE) {
            if($this->error_reporting === FALSE) {
                die('Invalid query: ' . $this->errno . ' '. $this->error. '. SQL: ' . $sql);
            } else {
                // 记录请求错误日志
                $url = htmlspecialchars($_SERVER['REQUEST_URI']);
                $time = time();
                $date = date('Y-m-d H:i:s', $time);
                $data = "<?php exit; ?> [DATE]$date [URL]$url [QUERY_ERRNO]". $this->errno ." [QUERY_ERROR]". $this->error ."
";
                error_log($data, 3, ROOT_PATH . '/data/log/'.date('Ymd', $time).'_mysql_error.php');
                // 停止程序执行
                //die('Invalid query.');
            }
        }
        return $this->result;
    }
    
    /**
     * 获取一条数据,如果没有数据返回空数组
     * @param array
     */
    public function get_one($sql) {
        $res = $this->query($sql);
        if ($res !== FALSE) {
            $row = $res->fetch_assoc();
            if(!empty($row)) {
                return $row;
            } else {
                return array();
            }
        } else {
            return array();
        }
    }
    
    /**
     * 获取所有数据,如果没有数据返回空数组
     */
    public function get_all($sql) {
        $res = $this->query($sql);
        if ($res !== FALSE) {
            //$arr = $result->fetch_all($resulttype); // MYSQLI_NUM,MYSQLI_ASSOC,MYSQLI_BOTH (PHP 5 >= 5.3.0)
            $arr = array();
            while($row = $res->fetch_assoc()) {
                $arr[] = $row;
            }
            $res->free(); // free result set
            return $arr;
        } else {
            return array();
        }
    }
    
    /**
     * +-----------------------------------
     * | 分页
     * +-----------------------------------
     * | 使用实例:
     * | $DB = new db_mysqli(...);
     * | $page_data = $DB->page("SELECT * FROM `test`"); // 获取数据
     * | $showpage = $DB->showpage(); // 生成分页链接
     * | print_r($page_data);
     * | echo $showpage; // 输出分页链接
     * +-----------------------------------
     */
    public function page($sql, $page_size = 10) {
        // 总记录数
        $this->page_count = $this->query($sql)->num_rows;
        if($this->page_count > 0) {
            // 总页数
            $this->page_all = ceil($this->page_count / $page_size);
            // 当前页
            $this->page_current = ( isset($_GET['page']) && (intval($_GET['page'])>0) ) ? intval($_GET['page']) : 1;
            if($this->page_current > $this->page_all) {
                $this->page_current = $this->page_all;
            }
            // 每页显示条数
            $this->page_size = $page_size;
            $sql = $sql . " LIMIT " . (($this->page_current-1) * $this->page_size) . ", " . $this->page_size;            
            return $this->get_all($sql);
        } else {
            return array();
        }
    }
    // 获取地址栏差数,主要用于分页时传递其他差数。$unset用于去掉不需要传值的参数,多个用,隔开
    public function geturl($unset = '') {
        $list = array();
        $keys = explode(',', $unset);
        foreach ($_GET as $key => $val) {
            if (!in_array($key, $keys)) {
                $list[] = $key.'='.urlencode($val);
            }
        }
        return implode('&amp;', $list);
    }
    // 显示分页,需要配合page()函数使用,$show_num=TRUE表示显示数字分页,否则显示首页、上一页、下一页、尾页
    public function showpage($show_num = TRUE) { 
        //echo "123123";
        $page = ''; 
        if($this->page_all > 1) { 
            $url = $this->geturl('page'); 
            $url = empty($url) ? '?page=' : '?'.$url.'&amp;page='; 
            if($this->page_current > 1) { 
                $page .= ' <a href="'.$url.'1">首页</a> <a href="'.$url.($this->page_current-1).'">上一页</a> '; 
            } else { 
                $page .= ' <span>首页</span> <span>上一页</span> '; 
            } 
     
            if($show_num) { 
                if($this->page_all < 6) { 
                    $arr = range(1, $this->page_all); 
                } else { 
                    if($this->page_current < 3) { 
                        $arr = range(1, 5); 
                    } elseif ( $this->page_current <= $this->page_all && $this->page_current > ($this->page_all - 3) ) { 
                        $arr = range(($this->page_all - 4), $this->page_all); 
                    } else { 
                        $arr = range(($this->page_current - 2), ($this->page_current + 2)); 
                    } 
                } 
                foreach($arr as $val) { 
                    if($val == $this->page_current) { 
                        $page .= ' <span>'.$val.'</span> '; 
                    } else { 
                        $page .= ' <a href="'.$url.$val.'">'.$val.'</a> '; 
                    } 
                } 
            } 
     
            if($this->page_current < $this->page_all) { 
                $page .= ' <a href="'.$url.($this->page_current + 1).'">下一页</a> <a href="'.$url.$this->page_all.'">尾页</a> '; 
            } else { 
                $page .= ' <span>下一页</span> <span>尾页</span> '; 
            } 
                 
            $page .= $this->page_count . "条记录 ". $this->page_current . "&#047;" . $this->page_all . "页 "; 
        } 
        return $page; 
    } 
}







视图

完成

原文地址:https://www.cnblogs.com/hellowzd/p/5395655.html