有id、标题、内容、创建时间
有创建时间、标题、(分页)
(分页)={$page}=
$count = $Form->count(); //计算总数 $Page = new Page($count, 5); $list = $Form->limit($Page->firstRow. ‘,‘ . $Page->listRows)->order(‘id desc‘)->select(); // 模拟设置分页额外传入的参数 $Page->parameter = ‘search=key&name=thinkphp‘; // 设置分页显示 $Page->setConfig(‘header‘, ‘条数据‘); $Page->setConfig(‘first‘, ‘<<‘); $Page->setConfig(‘last‘, ‘>>‘); $page = $Page->show(); $this->assign("page", $page);
$list = $Form->limit($Page->firstRow. ‘,‘ . $Page->listRows)->order(‘id desc‘)->select();
sql代码
-- phpMyAdmin SQL Dump -- version phpStudy 2014 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2014 年 12 月 04 日 19:08 -- 服务器版本: 5.5.38 -- PHP 版本: 5.3.28 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; /*!40101 SET @[email protected]@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- 数据库: `page` -- -- -------------------------------------------------------- -- -- 表的结构 `think_form` -- CREATE TABLE IF NOT EXISTS `think_form` ( `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` varchar(255) NOT NULL, `create_time` int(11) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ; -- -- 转存表中的数据 `think_form` -- INSERT INTO `think_form` (`id`, `title`, `content`, `create_time`) VALUES (1, ‘你好‘, ‘你好,这是内容‘, 0), (2, ‘好好‘, ‘士大夫士大夫‘, 0), (3, ‘圣达菲‘, ‘圣达菲‘, 0), (4, ‘圣达菲‘, ‘圣达菲‘, 0), (5, ‘圣达菲‘, ‘圣达菲‘, 0), (6, ‘圣达菲‘, ‘圣达菲‘, 0); /*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; /*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; /*!40101 SET [email protected]_COLLATION_CONNECTION */;
配置代码
<?php return array( //‘配置项‘=>‘配置值‘ //‘USERNAME‘=>‘admin‘, //赋值 //数据库配置信息 ‘URL_MODEL‘ => 1, ‘URL_PATHINFO_DEPR‘ => ‘/‘, ‘DB_TYPE‘ => ‘mysql‘, // 数据库类型 ‘DB_HOST‘ => ‘localhost‘, // 服务器地址 ‘DB_NAME‘ => ‘page‘, // 数据库名 ‘DB_USER‘ => ‘root‘, // 用户名 ‘DB_PWD‘ => ‘root‘, // 密码 ‘DB_PORT‘ => 3306, // 端口 ‘DB_PREFIX‘ => ‘think_‘, // 数据库表前缀 //其他项目配置参数 // ... ); ?>
控制器代码
<?php class IndexAction extends Action { public function index() { import("@.ORG.Page"); //导入分页类 $Form = M(‘Form‘); //实例化form表 $count = $Form->count(); //计算表内记录的总数 $Page = new Page($count, 5); //一页中的数量为5 $list = $Form->limit($Page->firstRow. ‘,‘ . $Page->listRows)->order(‘id desc‘)->select(); // 查询form表,根据id降序查询,limi用于限制查询结果数量,firstRow第一行,listRows表行 $Page->parameter = ‘search=key&name=thinkphp‘;//parameter参数 // 设置分页显示 $Page->setConfig(‘header‘, ‘条数据‘);//页面显示为:6条数据 $Page->setConfig(‘first‘, ‘<<‘); $Page->setConfig(‘last‘, ‘>>‘); $page = $Page->show(); $this->assign("page", $page); $this->assign("list", $list); $this->display(); } }
显示页代码
<!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> <title>ThinkPHP示例: 分页操作</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> *{ padding: 0; margin: 0;font-size:16px; font-family: "微软雅黑"} div{ padding: 3px 20px;} body{ background: #fff; color: #333;} h2{font-size:36px} div.result{border:1px solid #d4d4d4; background:#FFC;color:#393939; padding:8px 10px;float:auto; width:450px;margin:2px} a{text-decoration:none; color:gray;} a:hover{color:#F60;} div.page{border:1px solid #d4d4d4; background:#333;color:white; padding:5px 15px;float:auto; width:450px;margin:2px;text-align:right} </style> </head> <body> <div > <h2>ThinkPHP示例:分页操作</h2> <div class="result">可以更改配置文件中的<b>URL_MODEL</b>和<b>URL_PATHINFO_DEPR</b>参数查看分页链接的区别。</div> <table cellpadding=3 cellspacing=5> <volist name="list" id="vo"> <tr> <td style="border-bottom:1px solid silver;"><span style="color:gray">[ {$vo.create_time|date=‘Y-m-d H:i:s‘,###} ]</span> {$vo.title} </td> </tr> </volist> <tr> </tr> </table> <div class="result page">{$page}</div> </div> </body> </html>
原版:
sql代码
CREATE TABLE IF NOT EXISTS `think_form` ( `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` varchar(255) NOT NULL, `create_time` int(11) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
地址:
http://www.thinkphp.cn/extend/240.html
时间: 2024-10-10 16:12:21