php学习之Model类

<?php

$config = include 'config.php'; //引入数据库配置文件
$model = new Model($config);
//测试案例
// $saveData=['username'=>'张三','mobile'=>12334123];
// echo $model->table('user')->insert($saveData);
// var_dump($model->table('user')->where('id=5')->delete());
// var_dump($model->table('user')->limit('0,6')->field('username,mobile')->where('id>1')->order('id desc')->select());
// $updateData=['username'=>'张三'];
// var_dump($model->table('user')->where('id=3')->update($updateData));
// var_dump($model->table('user')->max('mobile'));
// var_dump($model->table('user')->getByUserName('张三'));
// var_dump($model->sql);

class Model
{
    //主机名
    protected $host;
    //用户名
    protected $user;
    //密码
    protected $pwd;
    //数据库名
    protected $dbname;
    //字符集
    protected $charset;
    //数据库前缀
    protected $prefix;

    //数据库连接资源
    protected $link;
    //数据表名       这里可以自己指定表名
    protected $tableName;

    //sql语句
    protected $sql;
    //操作数组   存放的就是所有的查询条件
    protected $options;

    /**
     * 构造方法,对成员变量进行初始化
     *
     * @param [type] $config
     */
    function __construct($config)
    {
        //对成员变量一一进行初始化
        $this->host = $config['DB_HOST'];
        $this->user = $config['DB_USER'];
        $this->pwd = $config['DB_PWD'];
        $this->dbname = $config['DB_NAME'];
        $this->charset = $config['DB_CHARSET'];
        $this->prefix = $config['DB_PREFIX'];

        //连接数据库
        $this->link = $this->connect();

        //得到数据表名  user===>UserModel
        $this->tableName = $this->getTableName();

        //初始化option数组
        $this->initOptions();
    }

    /**
     * 连接数据库
     *
     * @return void
     */
    protected function connect()
    {
        $link = mysqli_connect($this->host, $this->user, $this->pwd);
        if (!$link) {
            die('数据库连接失败');
        }
        //选择数据库
        mysqli_select_db($link, $this->dbname);
        //设置字符集
        mysqli_set_charset($link, $this->charset);
        //返回连接成功的资源
        return $link;
    }

    /**
     * 得到数据表名
     *
     * @return void
     */
    protected function getTableName()
    {
        //第一种,如果设置了成员变量,那么通过成员变量来得到表名
        if (!empty($this->tableName)) {
            return $this->prefix . $this->tableName;
        }
        //第二种,如果没有设置成员变量,那么通过类名来得到表名
        //得到当前类名字符串
        $className = get_class($this);
        //user  UserModel   goods  GoodsModel
        $table = strtolower(substr($className, 0, -5));
        return $this->prefix . $table;
    }

    /**
     * 初始化option数组
     *
     * @return void
     */
    protected function initOptions()
    {
        $arr = ['where', 'table', 'field', 'order', 'group', 'having', 'limit'];
        foreach ($arr as $value) {
            //将options数组中这些键对应的值全部清空
            $this->options[$value] = '';
            if ($value === 'table') {
                $this->options[$value] = $this->tableName;
            } elseif ($value == 'field') {
                $this->options[$value] = '*';
            }
        }
    }

    /**
     * field方法
     *
     * @param [type] $field
     * @return void
     */
    function field($field)
    {
        //如果不为空,再进行处理
        if (!empty($field)) {
            if (is_string($field)) {
                $this->options['field'] = $field;
            } elseif (is_array($field)) {
                $this->options['field'] = join(',', $field);
            }
        }
        return $this;
    }

    /**
     * //table方法
     *
     * @param [type] $table
     * @return void
     */
    function table($table)
    {
        if (!empty($table)) {
            $this->options['table'] = $this->prefix . $table;
        }
        return $this;
    }

    /**
     * where方法
     *
     * @param [type] $where
     * @return void
     */
    function where($where)
    {
        if (!empty($where)) {
            $this->options['where'] = 'where ' . $where;
        }
        return $this;
    }

    /**
     * group方法
     *
     * @param [type] $group
     * @return void
     */
    function group($group)
    {
        if (!empty($group)) {
            $this->options['group'] = 'group by ' . $group;
        }
        return $this;
    }

    /**
     * having方法
     *
     * @param [type] $having
     * @return void
     */
    function having($having)
    {
        if (!empty($having)) {
            $this->options['having'] = 'having ' . $having;
        }
        return $this;
    }

    /**
     * order方法
     *
     * @param [type] $order
     * @return void
     */
    function order($order)
    {
        if (!empty($order)) {
            $this->options['order'] = 'order by ' . $order;
        }
        return $this;
    }

    /**
     * limit方法
     *
     * @param [type] $limit
     * @return void
     */
    function limit($limit)
    {
        if (!empty($limit)) {
            if (is_string($limit)) {
                $this->options['limit'] = 'limit ' . $limit;
            } elseif (is_array($limit)) {
                $this->options['limit'] = 'limit ' . join(',', $limit);
            }
        }
        return $this;
    }

    /**
     * select方法
     *
     * @return void
     */
    function select()
    {
        //先预写一个带占位符的sql语句
        $sql = 'select %FIELD% from %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%';
        //将options中对应的值依次的替换上面的占位符
        $sql = str_replace(
            ['%FIELD%', '%TABLE%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%'],
            [
                $this->options['field'], $this->options['table'], $this->options['where'], $this->options['group'],
                $this->options['having'], $this->options['order'], $this->options['limit']
            ],
            $sql
        );
        //保存一份sql语句
        $this->sql = $sql;
        //执行sql语句
        return $this->query($sql);
    }

    /**
     * query
     *
     * @param [type] $sql
     * @return void
     */
    function query($sql)
    {
        //清空option数组中的值
        $this->initOptions();
        //执行sql语句
        $result = mysqli_query($this->link, $sql);
        //提取结果集存放到数组中
        if ($result && mysqli_affected_rows($this->link)) {
            while ($data = mysqli_fetch_assoc($result)) {
                $newData[] = $data;
            }
        }
        //返回结果集
        return $newData;
    }

    /**
     * exec
     *
     * @param [type] $sql
     * @param boolean $isInsert
     * @return void
     */
    function exec($sql, $isInsert = false)
    {
        //清空option数组中的值
        $this->initOptions();
        //执行sql语句
        $result = mysqli_query($this->link, $sql);
        if ($result && mysqli_affected_rows($this->link)) {
            //判断是否是插入语句,根据不同的语句返回不同的结果
            if ($isInsert) {
                return mysqli_insert_id($this->link);
            } else {
                return mysqli_affected_rows($this->link);
            }
        }
        return false;
    }

    function __get($name)
    {
        if ($name == 'sql') {
            return $this->sql;
        }
        return false;
    }

    /**
     * insert函数
     * insert into 表名(字段) value(值)
     *
     * @param [type] $data  关联数组,键就是字段名,值是字段值
     * @return void
     */
    function insert($data)
    {
        //处理值是字符串问题,两边需要添加单或双引号
        $data = $this->parseValue($data);
        //提取所有的键,即就是所有的字段
        $keys = array_keys($data);
        //提取所有的值
        $values = array_values($data);
        //增加数据的sql语句
        $sql = 'insert into %TABLE%(%FIELD%) values(%VALUES%)';
        $sql = str_replace(['%TABLE%', '%FIELD%', '%VALUES%'], [$this->options['table'], join(',', $keys), join(',', $values)], $sql);
        $this->sql = $sql;
        return $this->exec($sql, true);
    }

    /**
     * 传递进来一个数组,将数组中值为字符串的两边加上引号
     *
     * @param [type] $data
     * @return void
     */
    protected function parseValue($data)
    {
        //遍历数组,判断是否为字符串,若是字符串,将其两边添加引号
        foreach ($data as $key => $value) {
            if (is_string($value)) {
                $value = '"' . $value . '"';
            }
            $newData[$key] = $value;
        }
        //返回处理后的数组
        return $newData;
    }

    /**
     * 删除函数
     *
     * @return void
     */
    function delete()
    {
        //拼接sql语句
        $sql = 'delete from %TABLE% %WHERE%';
        $sql = str_replace(['%TABLE%', '%WHERE%'], [$this->options['table'], $this->options['where']], $sql);
        //保存sql语句
        $this->sql = $sql;
        //执行sql语句
        return $this->exec($sql);
    }

    /**
     * 更新函数
     * update 表名 set 字段名=字段值,字段名=字段值 where
     *
     * @param [type] $data
     * @return void
     */
    function update($data)
    {
        //处理$data数组中值为字符串加引号的问题
        $data = $this->parseValue($data);
        //将关联数组拼接为固定的格式  键=值,键=值
        $value = $this->parseUpdate($data);
        //准备sql语句
        $sql = 'update %TABLE% set %VALUE% %WHERE%';
        $sql = str_replace(['%TABLE%', '%VALUE%', '%WHERE%'], [$this->options['table'], $value, $this->options['where']], $sql);
        //保存sql语句
        $this->sql = $sql;
        //执行sql语句
        return $this->exec($sql);
    }

    /**
     * 将关联数组拼接为固定的格式
     *
     * @param [type] $data
     * @return void
     */
    protected function parseUpdate($data)
    {
        foreach ($data as $key => $value) {
            $newData[] = $key . '=' . $value;
        }
        return join(',', $newData);
    }

    /**
     * max函数
     *
     * @param [type] $field
     * @return void
     */
    function max($field)
    {
        //通过调用自己封装的方法进行查询
        $result = $this->field('max(' . $field . ') as max')->select();
        //select方法返回的是一个二维数组
        return $result[0]['max'];
    }

    /**
     * 析构方法
     */
    function __destruct()
    {
        mysqli_close($this->link);
    }

    /**
     * getByName   getByAge
     *
     * @param [type] $name
     * @param [type] $args
     * @return void
     */
    function __call($name, $args)
    {
        //获取前5个字符
        $str = substr($name, 0, 5);
        //获取后面的字段名
        $field = strtolower(substr($name, 5));
        //判断前五个字符是否是getby
        if ($str === 'getBy') {
            return $this->where($field . '="' . $args[0] . '"')->select();
        }
        return false;
    }
}

confing数据库配置文件,confing.php

<?php
return [
    'DB_HOST'=>'localhost',
    'DB_USER'=>'root',
    'DB_PWD'=>'123456',
    'DB_NAME'=>'test',
    'DB_CHARSET'=>'utf8',
    'DB_PREFIX'=>'t_',
];

 

 

<?php

$config = include 'config.php'; //引入数据库配置文件
$model = new Model($config);
//测试案例
// $saveData=['username'=>'张三','mobile'=>12334123];
// echo $model->table('user')->insert($saveData);
// var_dump($model->table('user')->where('id=5')->delete());
// var_dump($model->table('user')->limit('0,6')->field('username,mobile')->where('id>1')->order('id desc')->select());
// $updateData=['username'=>'张三'];
// var_dump($model->table('user')->where('id=3')->update($updateData));
// var_dump($model->table('user')->max('mobile'));
// var_dump($model->table('user')->getByUserName('张三'));
// var_dump($model->sql);

class Model
{
//主机名
protected $host;
//用户名
protected $user;
//密码
protected $pwd;
//数据库名
protected $dbname;
//字符集
protected $charset;
//数据库前缀
protected $prefix;

//数据库连接资源
protected $link;
//数据表名 这里可以自己指定表名
protected $tableName;

//sql语句
protected $sql;
//操作数组 存放的就是所有的查询条件
protected $options;

/**
* 构造方法,对成员变量进行初始化
*
* @param [type] $config
*/
function __construct($config)
{
//对成员变量一一进行初始化
$this->host = $config['DB_HOST'];
$this->user = $config['DB_USER'];
$this->pwd = $config['DB_PWD'];
$this->dbname = $config['DB_NAME'];
$this->charset = $config['DB_CHARSET'];
$this->prefix = $config['DB_PREFIX'];

//连接数据库
$this->link = $this->connect();

//得到数据表名 user===>UserModel
$this->tableName = $this->getTableName();

//初始化option数组
$this->initOptions();
}

/**
* 连接数据库
*
* @returnvoid
*/
protected function connect()
{
$link = mysqli_connect($this->host, $this->user, $this->pwd);
if (!$link) {
die('数据库连接失败');
}
//选择数据库
mysqli_select_db($link, $this->dbname);
//设置字符集
mysqli_set_charset($link, $this->charset);
//返回连接成功的资源
return $link;
}

/**
* 得到数据表名
*
* @returnvoid
*/
protected function getTableName()
{
//第一种,如果设置了成员变量,那么通过成员变量来得到表名
if (!empty($this->tableName)) {
return $this->prefix . $this->tableName;
}
//第二种,如果没有设置成员变量,那么通过类名来得到表名
//得到当前类名字符串
$className = get_class($this);
//user UserModel goods GoodsModel
$table = strtolower(substr($className, 0, -5));
return $this->prefix . $table;
}

/**
* 初始化option数组
*
* @returnvoid
*/
protected function initOptions()
{
$arr = ['where', 'table', 'field', 'order', 'group', 'having', 'limit'];
foreach ($arr as $value) {
//将options数组中这些键对应的值全部清空
$this->options[$value] = '';
if ($value === 'table') {
$this->options[$value] = $this->tableName;
} elseif ($value == 'field') {
$this->options[$value] = '*';
}
}
}

/**
* field方法
*
* @param [type] $field
* @returnvoid
*/
function field($field)
{
//如果不为空,再进行处理
if (!empty($field)) {
if (is_string($field)) {
$this->options['field'] = $field;
} elseif (is_array($field)) {
$this->options['field'] = join(',', $field);
}
}
return $this;
}

/**
* //table方法
*
* @param [type] $table
* @returnvoid
*/
function table($table)
{
if (!empty($table)) {
$this->options['table'] = $this->prefix . $table;
}
return $this;
}

/**
* where方法
*
* @param [type] $where
* @returnvoid
*/
function where($where)
{
if (!empty($where)) {
$this->options['where'] = 'where ' . $where;
}
return $this;
}

/**
* group方法
*
* @param [type] $group
* @returnvoid
*/
function group($group)
{
if (!empty($group)) {
$this->options['group'] = 'group by ' . $group;
}
return $this;
}

/**
* having方法
*
* @param [type] $having
* @returnvoid
*/
function having($having)
{
if (!empty($having)) {
$this->options['having'] = 'having ' . $having;
}
return $this;
}

/**
* order方法
*
* @param [type] $order
* @returnvoid
*/
function order($order)
{
if (!empty($order)) {
$this->options['order'] = 'order by ' . $order;
}
return $this;
}

/**
* limit方法
*
* @param [type] $limit
* @returnvoid
*/
function limit($limit)
{
if (!empty($limit)) {
if (is_string($limit)) {
$this->options['limit'] = 'limit ' . $limit;
} elseif (is_array($limit)) {
$this->options['limit'] = 'limit ' . join(',', $limit);
}
}
return $this;
}

/**
* select方法
*
* @returnvoid
*/
function select()
{
//先预写一个带占位符的sql语句
$sql = 'select %FIELD% from %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%';
//将options中对应的值依次的替换上面的占位符
$sql = str_replace(
['%FIELD%', '%TABLE%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%'],
[
$this->options['field'], $this->options['table'], $this->options['where'], $this->options['group'],
$this->options['having'], $this->options['order'], $this->options['limit']
],
$sql
);
//保存一份sql语句
$this->sql = $sql;
//执行sql语句
return $this->query($sql);
}

/**
* query
*
* @param [type] $sql
* @returnvoid
*/
function query($sql)
{
//清空option数组中的值
$this->initOptions();
//执行sql语句
$result = mysqli_query($this->link, $sql);
//提取结果集存放到数组中
if ($result && mysqli_affected_rows($this->link)) {
while ($data = mysqli_fetch_assoc($result)) {
$newData[] = $data;
}
}
//返回结果集
return $newData;
}

/**
* exec
*
* @param [type] $sql
* @paramboolean $isInsert
* @returnvoid
*/
function exec($sql, $isInsert = false)
{
//清空option数组中的值
$this->initOptions();
//执行sql语句
$result = mysqli_query($this->link, $sql);
if ($result && mysqli_affected_rows($this->link)) {
//判断是否是插入语句,根据不同的语句返回不同的结果
if ($isInsert) {
return mysqli_insert_id($this->link);
} else {
return mysqli_affected_rows($this->link);
}
}
return false;
}

function __get($name)
{
if ($name == 'sql') {
return $this->sql;
}
return false;
}

/**
* insert函数
* insert into 表名(字段) value(值)
*
* @param [type] $data 关联数组,键就是字段名,值是字段值
* @returnvoid
*/
function insert($data)
{
//处理值是字符串问题,两边需要添加单或双引号
$data = $this->parseValue($data);
//提取所有的键,即就是所有的字段
$keys = array_keys($data);
//提取所有的值
$values = array_values($data);
//增加数据的sql语句
$sql = 'insert into %TABLE%(%FIELD%) values(%VALUES%)';
$sql = str_replace(['%TABLE%', '%FIELD%', '%VALUES%'], [$this->options['table'], join(',', $keys), join(',', $values)], $sql);
$this->sql = $sql;
return $this->exec($sql, true);
}

/**
* 传递进来一个数组,将数组中值为字符串的两边加上引号
*
* @param [type] $data
* @returnvoid
*/
protected function parseValue($data)
{
//遍历数组,判断是否为字符串,若是字符串,将其两边添加引号
foreach ($data as $key => $value) {
if (is_string($value)) {
$value = '"' . $value . '"';
}
$newData[$key] = $value;
}
//返回处理后的数组
return $newData;
}

/**
* 删除函数
*
* @returnvoid
*/
function delete()
{
//拼接sql语句
$sql = 'delete from %TABLE% %WHERE%';
$sql = str_replace(['%TABLE%', '%WHERE%'], [$this->options['table'], $this->options['where']], $sql);
//保存sql语句
$this->sql = $sql;
//执行sql语句
return $this->exec($sql);
}

/**
* 更新函数
* update 表名 set 字段名=字段值,字段名=字段值 where
*
* @param [type] $data
* @returnvoid
*/
function update($data)
{
//处理$data数组中值为字符串加引号的问题
$data = $this->parseValue($data);
//将关联数组拼接为固定的格式 键=值,键=值
$value = $this->parseUpdate($data);
//准备sql语句
$sql = 'update %TABLE% set %VALUE% %WHERE%';
$sql = str_replace(['%TABLE%', '%VALUE%', '%WHERE%'], [$this->options['table'], $value, $this->options['where']], $sql);
//保存sql语句
$this->sql = $sql;
//执行sql语句
return $this->exec($sql);
}

/**
* 将关联数组拼接为固定的格式
*
* @param [type] $data
* @returnvoid
*/
protected function parseUpdate($data)
{
foreach ($data as $key => $value) {
$newData[] = $key . '=' . $value;
}
return join(',', $newData);
}

/**
* max函数
*
* @param [type] $field
* @returnvoid
*/
function max($field)
{
//通过调用自己封装的方法进行查询
$result = $this->field('max(' . $field . ') as max')->select();
//select方法返回的是一个二维数组
return $result[0]['max'];
}

/**
* 析构方法
*/
function __destruct()
{
mysqli_close($this->link);
}

/**
* getByName getByAge
*
* @param [type] $name
* @param [type] $args
* @returnvoid
*/
function __call($name, $args)
{
//获取前5个字符
$str = substr($name, 0, 5);
//获取后面的字段名
$field = strtolower(substr($name, 5));
//判断前五个字符是否是getby
if ($str === 'getBy') {
return $this->where($field . '="' . $args[0] . '"')->select();
}
return false;
}
}

转载于:https://www.cnblogs.com/shengChristine/p/10966007.html

<?php /** * 产品属性模型 */ class s_model extends CI_MODEL { function all($table,$where='',$select=0,$id='',$like=0,$a=0,$b=0){ $where = isset($where)&&!empty($where)?$where:''; $id = isset($id)&&!empty($id)?" order by ".$id." desc ":''; $a = isset($a)&&!empty($a)?$a:0; $b = isset($b)&&!empty($b)?$b:999999; $select = isset($select)&&!empty($select)?$select:'*'; $like = isset($like)&&!empty($like)?$like:'';//and cat_type like '%{$_POST['cat_type']}%' $sql = "select ".$select." from ".$table.$where.$like.$id." limit ".$a.",".$b.""; return $this->db->query($sql)->result_array(); //查找 } function row($table,$where='',$select=0,$id='',$like=0,$a=0,$b=0){ $where = isset($where)&&!empty($where)?$where:''; $id = isset($id)&&!empty($id)?" order by ".$id." desc ":''; $a = isset($a)&&!empty($a)?$a:0; $b = isset($b)&&!empty($b)?$b:999999; $select = isset($select)&&!empty($select)?$select:'*'; $like = isset($like)&&!empty($like)?$like:'';//and cat_type like '%{$_POST['cat_type']}%' $sql = "select ".$select." from ".$table.$where.$like.$id." limit ".$a.",".$b.""; return $this->db->query($sql)->row_array(); //查找 } function num($table,$where='',$select=0,$id='',$like=0,$a=0,$b=0){ $where = isset($where)&&!empty($where)?$where:''; $id = isset($id)&&!empty($id)?" order by ".$id." desc ":''; $a = isset($a)&&!empty($a)?$a:0; $b = isset($b)&&!empty($b)?$b:999999; $select = isset($select)&&!empty($select)?$select:'*'; $like = isset($like)&&!empty($like)?$like:'';//and cat_type like '%{$_POST['cat_type']}%' $sql = "select ".$select." from ".$table.$where.$like.$id." limit ".$a.",".$b.""; return $this->db->query($sql)->num_rows(); //查找 } function uer_quanxian($id){ //$_GET['id']传过来的控制器文件夹 控制器名字 方法名字 中间没有/ $sq = "select yz_qx_href from xf_qx where id=".$_SESSION['founder'].""; $res = $this->db->query($sq)->row_array(); $res = explode(',', $res['yz_qx_href']); //p($res);exit(); foreach ($res as $key => &$value) { $sqss = "select id,yz_ci_href from xf_qx_ci where id=".$value.""; $value= $this->db->query($sqss)->result_array(); } $k = 0; foreach ($res as $key => $val) { foreach ($val as $key2 => $val2) { $newhello[$k]['id'] = $val2['id']; $newhello[$k]['yz_ci_href'] = $val2['yz_ci_href']; $k++; } } foreach ($newhello as $key => $value) { if ($value['yz_ci_href']== $_GET['id']) { echo $_GET['id']; }else{ echo "2"; } } } function getfirstchar($s0){ $firstchar_ord=ord(strtoupper($s0{0})); if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0}; $s=iconv("UTF-8","gb2312", $s0); $asc=ord($s{0})*256+ord($s{1})-65536; if($asc>=-20319 and $asc<=-20284)return "A"; if($asc>=-20283 and $asc<=-19776)return "B"; if($asc>=-19775 and $asc<=-19219)return "C"; if($asc>=-19218 and $asc<=-18711)return "D"; if($asc>=-18710 and $asc<=-18527)return "E"; if($asc>=-18526 and $asc<=-18240)return "F"; if($asc>=-18239 and $asc<=-17923)return "G"; if($asc>=-17922 and $asc<=-17418)return "H"; if($asc>=-17417 and $asc<=-16475)return "J"; if($asc>=-16474 and $asc<=-16213)return "K"; if($asc>=-16212 and $asc<=-15641)return "L"; if($asc>=-15640 and $asc<=-15166)return "M"; if($asc>=-15165 and $asc<=-14923)return "N"; if($asc>=-14922 and $asc<=-14915)return "O"; if($asc>=-14914 and $asc<=-14631)return "P"; if($asc>=-14630 and $asc<=-14150)return "Q"; if($asc>=-14149 and $asc<=-14091)return "R"; if($asc>=-14090 and $asc<=-13319)return "S"; if($asc>=-13318 and $asc<=-12839)return "T"; if($asc>=-12838 and $asc<=-12557)return "W"; if($asc>=-12556 and $asc<=-11848)return "X"; if($asc>=-11847 and $asc<=-11056)return "Y"; if($asc>=-11055 and $asc<=-10247)return "Z"; return null; } //查找 public function getAll($table='',$where=array(),$select=''){ return $this->db->select($select)->where($where)->get($table)->result_array(); }//添加成功 function add($a){ $b= site_url($a); $this->halt($b,"添加成功",0); } function edit($a){ $b= site_url($a); $this->halt($b,"修改成功",0); } function del($a){ $b= site_url($a); $this->halt($b,"删除成功",0); } function view($a){ $b= site_url($a); $this->halt($b,"操作成功",0); } function view_del($a){ $b= site_url($a); $this->halt($b,"操作失败",4); }//查询 function select(){ $res=$this->db->get('yz_new_cate')->result_array(); foreach($res as $key=>$val){ $row=$this->db->where('cat_id',$val['cat_pid'])->get('yz_new_cate')->result_array(); if($row){ $res[$key]['father_name']=$row[0]['cat_name']; } } }//递归 function gui($arr,$html='|--',$pid=0,$level=0){ $str=array(); foreach($arr as $v){ if($v['cat_pid']==$pid){ $v['level']=$level + 1; $v['html']=str_repeat($html,$level); $str[]=$v; $str=array_merge($str,$this->gui($arr,$html,$v['cat_id'],$level+1)); } } return $str; } //递归是用方法cat_id 主id //top_id 主id是1 子都是1 //cat_pid 关联的id 主id是1则他是1 主id是2 下一集是2 //cat_level 分级id 总为1级 子 为2 在往下为3 /** * @CopyRight * @WebSite * @Author turingphp-zy * @Brief 图片放到根目录 * $msgtype 0 成功 1警告 2失败 **/// 第三个参数等于0和1好用 等于2不太好用 //$b= site_url('yemian/apption/get_list'); // $this->s_model->halt($b,"增加成功",0); function halt($forwardurl,$message,$msgtype) {$a=base_url('imagess/return.gif'); $c=base_url('imagess/donghua.gif'); //$c=base_url('imagess/return.gif'); //define('STYLE_PATH',str_replace(base_url(),'',base_url())); //define('STYLE_PATH',str_replace($_SERVER['DOCUMENT_ROOT'],'',ROOT_DIR)); ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>温馨提示</title> <style> a{text-decoration: none;} .alert{background-color:#f1f1f1; width:495px;margin:50px auto; font-size:12px; line-height:24px;} .alert .alert_body{ border:1px solid #cbcbcb;background-color:#fff; width:475px; height:143px; position:relative; top:-5px; left:-5px; padding:10px;} .alert .alert_body h3{font-size:14px; font-weight:bold; margin:0;} .alert .alert_body .alertcont{margin:15px 0 0 85px; background:url(<?php echo base_url('imagess/m_err.png')?>) left center no-repeat; padding:5px 50px; line-height:18px; color:#666; min-height:30px; _height:30px;} .alert .alert_body .alertcont a{color:#000; text-decoration:none;} .alert .alert_body .alertcont span{font-size:12px; font-weight:bold; color:#000;} .alert .alert_body .btn{text-align:center; padding-top:0px;} .alert .alert_body .btn img{border:0;} .alert .alert_body .pi2{background:url(<?php echo base_url('imagess/m_err.png')?>) left center no-repeat;} .alert .alert_body .pi1{background:url(<?php echo base_url('imagess/m_acc.png')?>) left center no-repeat; padding-left:55px;} .message{display:block;position:absolute;top:0;left:30%; left:50%;/*FF IE7*/ top: 50%;/*FF IE7*/ margin-left:-240px!important;/*FF IE7 该值为本身宽的一半 */ margin-top:-70px!important;/*FF IE7 该值为本身高的一半*/ margin-top:0; position:fixed!important;/*FF IE7*/ position:absolute;/*IE6*/ _top: expression(eval(document.compatMode && document.compatMode=='CSS1Compat') ? documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :/*IE6*/ document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);/*IE5 IE5.5*/} .message .alert_body{ padding:0; height:163px; width:495px; border:1px solid #dfdfdf;} .message .alert_body h3{background-color:#fbfbe7; padding:3px 15px;} </style> </head> <body> <div class="alert message"> <div class="alert_body"> <?php if(intval($msgtype)==1){ $css = "alertcont"; }elseif(intval($msgtype)==2){ $css = "alertcont pi2"; }elseif(intval($msgtype)==4){ $css = "alertcont"; }elseif(intval($msgtype)==5){ $css = "alertcont"; }else{ $css = "alertcont pi1"; } ?> <h3>温馨提示</h3> <p class="<?php echo $css;?>"> <span><?php echo $message?></span> </p> <?php switch(intval($msgtype)){ case 0: echo("<meta http-equiv=\"refresh\" content=\"2;url=".$forwardurl."\">"); echo("<p style=\"font-align:center;\" align=\"center\"><a href=\"".$forwardurl."\"><img src=$c />秒后 如果你的浏览器没有自动跳转,请点击此链接</a></p>"); break; case 3: echo("<meta http-equiv=\"refresh\" content=\"2;url=".$forwardurl."\">"); echo("<p style=\"font-align:center;\" align=\"center\"><a href=\"".$forwardurl."\">2秒后 如果你的浏览器没有自动跳转,请点击此链接</a></p>"); break; case 4: echo("<meta http-equiv=\"refresh\" content=\"2;url=".$forwardurl."\">"); echo("<p style=\"font-align:center;\" align=\"center\"><a href=\"".$forwardurl."\">2秒后 如果你的浏览器没有自动跳转,请点击此链接</a></p>"); break; case 1: echo("<p class=\"btn\"><a href=\"javascript:history.go(-1);\"><img src=$a /> </a></p>"); break; case 2: echo("<p class=\"btn\"><a href=\"javascript:history.go(-1);\"><img src=$a/> </a></p>"); break; default: break; case 5: echo("<meta http-equiv=\"refresh\" content=\"0;url=".$forwardurl."\">"); break; case 6: echo("<p style=\"font-align:center;\" align=\"center\"><a href=\"javascript:top.location='".$forwardurl."'\">2秒后 如果你的浏览器没有自动跳转,请点击此链接</a></p>"); break; } ?> </div> </div> </body> </html> <?php exit; } } ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值