«

ThinkPHP3.2初学

ljierui 发布于 阅读:168 技术杂谈


Thinkphp 3.2.3

一、安装

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------

// 应用入口文件

// 检测PHP环境
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    die('require PHP > 5.3.0 !');
}

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG', true);

// 定义应用目录
//define('APP_PATH', './Application/');
//修改成自己定义的目录
define('APP_PATH', './App/');

// 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';

// 亲^_^ 后面不需要任何代码了 就是如此简单

二、目录结构分析

1、MVC

M - Model -> 负责数据库操作
V - View -> 用来显示界面
C - Contol -> 用来控制连接Modle层和View层

2、App目录下文件结构

├─Common (用来保存一些配置信息,比如数据库配置)
│  ├─Common
│  └─Conf
├─Home (一般用于前台显示,里面重要的目录就是Controller和View)
│  ├─Common
│  ├─Conf
│  ├─Controller
│  ├─Model
│  └─View
└─Runtime (用于保存一个运行时的日志文件信息)
    ├─Cache
    │  └─Home
    ├─Data
    ├─Logs
    │  └─Home
    └─Temp

3、创建后台文件

<?php
// 修改成Admin
namespace Admin\Controller; 

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }
}

三、ThinkPHP路由方式

1、index.php路由解析

http://localhost/test.com/index.php/home/index/index
等价于
http://localhost/test.com/index.php?m=home&v=Index&a=index
解释
http://localhost/test.com/index.php?m=模块名&v=控制器名&a=调用的方法名

2、修改显示页面

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        echo "你好! ThinkPHP 3.2.3";
    }
}

3、调用自己写的方法

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        echo "你好! ThinkPHP 3.2.3";
    }

    public function hello(){
        echo "进入了hello 方法";
    }
}
两种方式
http://localhost/test.com/index.php?m=home&v=Index&a=hello
http://localhost/test.com/index.php/home/Index/hello

4、调用自己写的控制器名

<?php
namespace Home\Controller;

use Think\Controller;

class CateController extends Controller
{
    public function index()
    {
        echo "进入了Cate控制器";
    }

    public function haha(){
        echo "访问了Cate控制器中的haha方法";
    }
}

四、模板的使用

1、View目录

2、修改IndexController.class.php

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 调用display方法
        $this->display();
    }

    public function hello(){
        echo "进入了hello 方法";
    }
}

3、分析display方法

    /**
     * 模板显示 调用内置的模板引擎显示方法,
     * @access protected
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @param string $charset 输出编码
     * @param string $contentType 输出类型
     * @param string $content 输出内容
     * @param string $prefix 模板缓存前缀
     * @return void
     */
    protected function display($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '')
    {
        $this->view->display($templateFile, $charset, $contentType, $content, $prefix);
    }

4、在View目录下添加新文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>

<h1>hello Index!</h1>
<h2>这是首页</h2>
</body>
</html>

5、通过dispay方法调用模板

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 调用display方法
        $this->display();
    }

    public function hello(){
        $this->display(Index);
    }
}

6、总结

五、__PUBLIC__ 关键字

六、简易留言板

1、把前端JS文件放到Public中

2、把需要的index.html文件放到view目录下

3、头部模板分离

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0017)http://www.k.com/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Cache-Control" content="no-transform">

    <title>简易留言板</title>
    <meta content="简易、留言板、THINKPHP、入门" name="keywords">
    <meta content="新手可以看的简易留言板" name="description">
    <link href="__PUBLIC__/Index/default.css" media="screen" rel="stylesheet" type="text/css">
    <script src="__PUBLIC__/Index/jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
    <div class="logo"><a href="http://localhost/test.com/">LYB</a></div>
    <div class="headerLink">
        <a href="/">首页</a>
        <a href="{:U('Home/Index/pub')}">发布留言</a>
        <a href="{:U('Home/Index/manage')}">管理留言</a>
    </div>
</div>

4、包含头部文件

<include file="common/header" />

5、修改控制器

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 调用display方法
        $this->display();
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $this->display();
    }

    //编辑留言
    public function edit(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $this->display();
    }
}

6、在模板中使用U函数

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0017)http://www.k.com/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Cache-Control" content="no-transform">

    <title>简易留言板</title>
    <meta content="简易、留言板、THINKPHP、入门" name="keywords">
    <meta content="新手可以看的简易留言板" name="description">
    <link href="__PUBLIC__/Index/default.css" media="screen" rel="stylesheet" type="text/css">
    <script src="__PUBLIC__/Index/jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
    <div class="logo"><a href="http://localhost/test.com/">LYB</a></div>
    <div class="headerLink">
        <a href="/">首页</a>
        <a href="{:U('Home/Index/pub')}">发布留言</a>
        <a href="{:U('Home/Index/manage')}">管理留言</a>
    </div>
</div>

7、配置数据库

<?php
return array(
    //'配置项'=>'配置值'
    /* 数据库设置 */
    'DB_TYPE'                => 'mysql', // 数据库类型
    'DB_HOST'                => 'localhost', // 服务器地址
    'DB_NAME'                => 'thinkphp33', // 数据库名
    'DB_USER'                => 'root', // 用户名
    'DB_PWD'                 => 'root', // 密码
    'DB_PORT'                => '3306', // 端口
    'DB_PREFIX'              => '', // 数据库表前缀
    'DB_PARAMS'              => array(), // 数据库连接参数
    'DB_DEBUG'               => true, // 数据库调试模式 开启后可以记录SQL日志
    'DB_FIELDS_CACHE'        => true, // 启用字段缓存
    'DB_CHARSET'             => 'utf8', // 数据库编码默认采用utf8
    'DB_DEPLOY_TYPE'         => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
    'DB_RW_SEPARATE'         => false, // 数据库读写是否分离 主从式有效
    'DB_MASTER_NUM'          => 1, // 读写分离后 主服务器数量
    'DB_SLAVE_NO'            => '', // 指定从服务器序号
);

8、使用M函数首页显示数据

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        //dump($comment);
        // 查询数据
        $data  = $comment->select();
        dump($data);
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $this->display();
    }

    //编辑留言
    public function edit(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $this->display(manage);
    }
}

9、使用自带功能实现分页

'PAGE_COUNT'             =>10,
echo C('PAGE_COUNT');
            <foreach name="data" item="vo" >
            <tr class="listtr" style="line-height:32px;">
                    <td class="listtd alignCenter">{$vo.ip}</td>
                    <td class="listtd">{$vo.title}</td>
                    <td class="listtd">{$vo.content}</td>
                    <td class="listtd alignCenter">{$vo.time}</td>
                </tr>

            </foreach>
<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 实现分页
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $this->display();
    }

    //编辑留言
    public function edit(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $this->display(manage);
    }
}

10、发布留言

<?php
function get_client_ip($type = 0)
{
    $type = $type ? 1 : 0;
    static $ip = NULL;
    if ($ip !== NULL) {
        return $ip[$type];
    }
    if (isset($_SERVER['HTTP_X_REAL_IP'])) {
        //nginx 代理模式下,获取客户端真实IP
        $ip = $_SERVER['HTTP_X_REAL_IP'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        //客户端的ip
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        //浏览当前页面的用户计算机的网关
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos) unset($arr[$pos]);
        $ip = trim($arr[0]);
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        //浏览当前页面的用户计算机的ip地址
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
    //发布留言
    public function pub(){
        $ip = get_client_ip();
        dump($ip);
        exit();
    }
<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 实现分页
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $ip = get_client_ip();
//        dump($ip);
//        exit();
        // I函数,用来接收所有的request请求
        // 判断是否为空
        if (!empty(I("submitLy"))){
            //插入数据库
            $ip = I("ip");
            $title = I("title");
            $content = I("content");
            $time = time();

            // 要和表名对应
            $data = array(
                'ip' => $ip,
                'title' =>$title,
                'content' => $content,
                'time'=>$time
            );
            // 获取数据库对象
            $comment = M("Comment");
            // 插入语句
            $result = $comment->add($data);
            //dump($result);
            $result>0?$this>$this->success("留言成功",U('Home/Index/index'),2):$this->error("失败");

        }else{
            // 如果为空
            $this->assign('ip',$ip);
            $this->display();
        }
    }

    //编辑留言
    public function edit(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $this->display(manage);
    }
}

11、管理留言

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 实现分页
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $ip = get_client_ip();
//        dump($ip);
//        exit();
        // I函数,用来接收所有的request请求
        // 判断是否为空
        if (!empty(I("submitLy"))){
            //插入数据库
            $ip = I("ip");
            $title = I("title");
            $content = I("content");
            $time = time();

            // 要和表名对应
            $data = array(
                'ip' => $ip,
                'title' =>$title,
                'content' => $content,
                'time'=>$time
            );
            // 获取数据库对象
            $comment = M("Comment");
            // 插入语句
            $result = $comment->add($data);
            //dump($result);
            $result>0?$this>$this->success("留言成功",U('Home/Index/index'),2):$this->error("失败");

        }else{
            // 如果为空
            $this->assign('ip',$ip);
            $this->display();
        }
    }

    //编辑留言
    public function edit(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }
}
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------

// 应用入口文件

// 检测PHP环境
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    die('require PHP > 5.3.0 !');
}

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG', true);

// 定义应用目录
define('APP_PATH', './App/');

// 定义网址路径
define('SITE_URL', 'http://localhost/test.com/index.php/');

// 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';

// 亲^_^ 后面不需要任何代码了 就是如此简单
<include file="common/header" />

<div id="main">

    <script type="text/javascript">
        $(function(){
            $(".headerLink a:eq(2)").addClass('headerLinkSelect');
        });
    </script>
    <table>
            <tbody><tr class="listtr">
                <th class="listth" style="width: 10%;">留言IP</th>
                <th class="listth">留言主题</th>
                <th class="listth">留言内容</th>
                <th class="listth" style="width: 18%;">留言时间</th>
                <th class="listth" style="width: 10%;">操作</th>
            </tr>

            <foreach name="data" item="vo" >
            <tr class="listtr" style="line-height:32px;">
                    <td class="listtd alignCenter">{$vo.ip}</td>
                    <td class="listtd">{$vo.title}</td>
                    <td class="listtd">{$vo.content}</td>
                    <td class="listtd alignCenter">{$vo.time}</td>
                    <td class="listtd alignCenter">
                        <a href="{$Think.SITE_URL}/Home/Index/edit/?id={$vo.id}">修改</a>&nbsp;&nbsp;
                        <a href="{$Think.SITE_URL}/Home/Index/del/?id={$vo.id}">删除</a></td>
                </tr>

            </foreach>
            <tr class="listtr">
                <td class="listtd" style="background-color: #f0f0f0;" colspan="5">

                    <div>  {$page} </div>
                </td>
            </tr>
        </tbody></table>

</div>
</body></html>
<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 实现分页
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $ip = get_client_ip();
//        dump($ip);
//        exit();
        // I函数,用来接收所有的request请求
        // 判断是否为空
        if (!empty(I("submitLy"))){
            //插入数据库
            $ip = I("ip");
            $title = I("title");
            $content = I("content");
            $time = time();

            // 要和表名对应
            $data = array(
                'ip' => $ip,
                'title' =>$title,
                'content' => $content,
                'time'=>$time
            );
            // 获取数据库对象
            $comment = M("Comment");
            // 插入语句
            $result = $comment->add($data);
            //dump($result);
            $result>0?$this>$this->success("留言成功",U('Home/Index/index'),2):$this->error("失败");

        }else{
            // 如果为空
            $this->assign('ip',$ip);
            $this->display();
        }
    }

    //编辑留言
    public function edit(){
        $ip = get_client_ip();

        if (!empty(I(submitLy))){
            $id = I("id");
            $time = date("Y-m-d H:i:s",time());
            $data = array(
                'ip' => I('ip'),
                'title' =>I('title'),
                'content' => I('content'),
                'time'=> $time
            );

            $comment = M("Comment");
            $result = $comment->where("id=$id")->save($data);
            $result>0?$this>$this->success("修改成功",U('Home/Index/manage'),2):$this->error("失败");

        }else{
            $id = I("id");
            //查出数据
            $comment = M('Comment');
            $data = $comment->where("id=$id")->find();
            $this->assign('ip',$ip);
            $this->assign('data',$data);
            $this->display();
        }

    }

    //删除留言
    public function del(){
        $this->display();
    }

    //管理留言
    public function manage(){
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }
}

12、删除留言

<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        // 实现分页
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function hello(){
        $this->display(Index);
    }

    //发布留言
    public function pub(){
        $ip = get_client_ip();
//        dump($ip);
//        exit();
        // I函数,用来接收所有的request请求
        // 判断是否为空
        if (!empty(I("submitLy"))){
            //插入数据库
            $ip = I("ip");
            $title = I("title");
            $content = I("content");
            $time = time();

            // 要和表名对应
            $data = array(
                'ip' => $ip,
                'title' =>$title,
                'content' => $content,
                'time'=>$time
            );
            // 获取数据库对象
            $comment = M("Comment");
            // 插入语句
            $result = $comment->add($data);
            //dump($result);
            $result>0?$this>$this->success("留言成功",U('Home/Index/index'),2):$this->error("失败");

        }else{
            // 如果为空
            $this->assign('ip',$ip);
            $this->display();
        }
    }

    //编辑留言
    public function edit(){
        $ip = get_client_ip();

        if (!empty(I(submitLy))){
            $id = I("id");
            $time = date("Y-m-d H:i:s",time());
            $data = array(
                'ip' => I('ip'),
                'title' =>I('title'),
                'content' => I('content'),
                'time'=> $time
            );

            $comment = M("Comment");
            $result = $comment->where("id=$id")->save($data);
            $result>0?$this>$this->success("修改成功",U('Home/Index/manage'),2):$this->error("失败");

        }else{
            $id = I("id");
            //查出数据
            $comment = M('Comment');
            $data = $comment->where("id=$id")->find();
            $this->assign('ip',$ip);
            $this->assign('data',$data);
            $this->display();
        }

    }

    //删除留言
    public function del(){
        $id = I("id");
        $comment = M('Comment');
        $result = $comment->where("id=$id")->delete();
        $result>0?$this>$this->success("删除成功",U('Home/Index/manage'),2):$this->error("失败");
    }

    //管理留言
    public function manage(){
        $limit = C('PAGE_COUNT');
        // 显示数据 M("表名")
        // 实例化Comment对象
        $comment = M("Comment");
        $count      = $comment->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $comment->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }
}

代码审计

推荐阅读: