thinkphp 3.1 这个查询怎么进行分页
$sql="SELECTphoto,userid,relname,username,Phone,qq,b.nameASSex,c.nameASAge,d.nameASma...
$sql="SELECT photo,userid,relname,username,Phone,qq, b.name AS Sex,c.name AS Age,d.name AS marriage,
e.name AS height,f.name AS weight,g.name AS school,h.name AS salary,i.name AS house,
j.name AS profession,k.name AS zage1,l.name AS zage2,m.name AS Zheight1,n.name AS Zheight2,
o.name AS zschool,p.name AS zmoney,r.name AS place
FROM ty_userinfo a
LEFT JOIN ty_class2 b ON a.Sex=b.id
LEFT JOIN ty_class2 c ON a.Age=c.id
LEFT JOIN ty_class2 d ON a.marriage=d.id
LEFT JOIN ty_class2 e ON a.height=e.id
LEFT JOIN ty_class2 f ON a.weight=f.id
LEFT JOIN ty_class2 g ON a.school=g.id
LEFT JOIN ty_class2 h ON a.salary=h.id
LEFT JOIN ty_class2 i ON a.house=i.id
LEFT JOIN ty_class2 j ON a.profession=j.id
LEFT JOIN ty_class2 k ON a.zage1=k.id
LEFT JOIN ty_class2 l ON a.zage2=l.id
LEFT JOIN ty_class2 m ON a.Zheight1=m.id
LEFT JOIN ty_class2 n ON a.Zheight2=n.id
LEFT JOIN ty_class2 o ON a.zschool=o.id
LEFT JOIN ty_class2 p ON a.zmoney=p.id
LEFT JOIN ty_area r ON a.place=r.id
WHERE (1=1)
";
if(!empty($sex))
{
$sql .=" and Sex= ".$sex;
}
if(!empty($marriage))
{
$sql .=" and marriage = ".$marriage;
}
if (!empty($age1)) {
$sql .=" and Age >= ".$age1;
}
if (!empty($age2)) {
$sql .=" and Age <= ".$age2;
}
if (!empty($place)) {
$sql .=" and place = ".$place[1];
}
$info=M('ty_userinfo')->query($sql);
$this->info=$info; 展开
e.name AS height,f.name AS weight,g.name AS school,h.name AS salary,i.name AS house,
j.name AS profession,k.name AS zage1,l.name AS zage2,m.name AS Zheight1,n.name AS Zheight2,
o.name AS zschool,p.name AS zmoney,r.name AS place
FROM ty_userinfo a
LEFT JOIN ty_class2 b ON a.Sex=b.id
LEFT JOIN ty_class2 c ON a.Age=c.id
LEFT JOIN ty_class2 d ON a.marriage=d.id
LEFT JOIN ty_class2 e ON a.height=e.id
LEFT JOIN ty_class2 f ON a.weight=f.id
LEFT JOIN ty_class2 g ON a.school=g.id
LEFT JOIN ty_class2 h ON a.salary=h.id
LEFT JOIN ty_class2 i ON a.house=i.id
LEFT JOIN ty_class2 j ON a.profession=j.id
LEFT JOIN ty_class2 k ON a.zage1=k.id
LEFT JOIN ty_class2 l ON a.zage2=l.id
LEFT JOIN ty_class2 m ON a.Zheight1=m.id
LEFT JOIN ty_class2 n ON a.Zheight2=n.id
LEFT JOIN ty_class2 o ON a.zschool=o.id
LEFT JOIN ty_class2 p ON a.zmoney=p.id
LEFT JOIN ty_area r ON a.place=r.id
WHERE (1=1)
";
if(!empty($sex))
{
$sql .=" and Sex= ".$sex;
}
if(!empty($marriage))
{
$sql .=" and marriage = ".$marriage;
}
if (!empty($age1)) {
$sql .=" and Age >= ".$age1;
}
if (!empty($age2)) {
$sql .=" and Age <= ".$age2;
}
if (!empty($place)) {
$sql .=" and place = ".$place[1];
}
$info=M('ty_userinfo')->query($sql);
$this->info=$info; 展开
4个回答
展开全部
一、首先需要在MsgManage控制器中加入分页方法
知识点:
1、count函数的试用
2、Page类实例化操作及相关参数了解
3、limit函数了用
4、show函数了解
编辑文件admin/Lib/Action/MsgManageAction.class.php
代码如下:
复制代码代码如下:
class MsgManageAction extends CommonAction {
public function index(){
import('ORG.Util.Page');
//import调用的是message/ThinkPHP框架目录下的扩展包Extend/Library/ORG/Util/中的Page.class.php类文件
$count = M('board')->count();
//调用board库,取出所有数据条数
$page = new Page($count ,10);
//实例化Page类,其中第一个参数为显示条数的总数,每次取出十条,也就是下面$page->listRows的值
$limit = $page->firstRow . ',' . $page->listRows;
//$page->firstRow为查找的起始条数,默认为0,如果$page->listRows为10,那么第2页的$page->firstRow就为10,以此类推
$board = M('board')->order('time DESC')->limit($limit)->select();
//注意,这里较之前的版本添加了->limit($limit)
$this->board = $board;
$this->page = $page->show();
//将$page->show()通过show方法解析$page内容显示并赋值给模板变量,供模板调用
$this->display();
}
Public function delete(){
$id = I('id','','intval');
if(M('board')->delete($id)){
$this->success('删除成功',U('index'));
}else{
$this->error('删除失败');
}
}
}
show方法是3.1版本才有的一个新功能
ThinkPHP中页面输出的过程是读取模板文件,然后进行模板解析(也支持调用第三方模板引擎解析),但是有一些情况,我们并没有定义模板文件,或者把模板文件保存在数据库里面,那么这个时候进行页面输出的时候,我们是无法进行模板文件读取的,3.1版本则针对这样的情况增加了内容解析输出的功能。
内置的模板引擎也进行了完善,如果传入的模板文件不存在的话,则会认为是传入的模板解析内容,因此,3.1的View类和Action类也做了一些改进。
display方法用于模板文件渲染输出,show方法则用于模板内容渲染输出,并且show方法仍然支持内容解析功能
具体内容可参考:ThinkPHP3.1新特性 内容解析输出
二、在模板文件中加入分页模块
知识点:
1、td单元格合并
2、$page变量调用显示
编辑文件:admin/Tpl/MsgManage/index.html,加入一段tr用来显示分页相关,代码如下:
复制代码代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Message Board BackGround</title>
</head>
<body>
<table class="table" border="1">
<tr>
<th>ID</th>
<th>发布者</th>
<th>内容</th>
<th>发布时间</th>
<th>操作</th>
</tr>
<foreach name='board' item='b'>
<tr>
<td>{$b.id}</td>
<td>{$b.username}</td>
<td>{$b.content}</td>
<td>{$b.time|date='y-m-d H:i',###}</td>
<td><a href="{:U('admin.php/MsgManage/delete',array('id' => $b['id'])),''}">删除</a></td>
</tr>
</foreach>
//新增tr代码短
<tr>
<td colspan='5' align='center'>
//将5个单元格合并,并且居中显示
{$page}
//显示控制器中$this->page内容
</td>
</tr>
</table>
</body>
</html>
知识点:
1、count函数的试用
2、Page类实例化操作及相关参数了解
3、limit函数了用
4、show函数了解
编辑文件admin/Lib/Action/MsgManageAction.class.php
代码如下:
复制代码代码如下:
class MsgManageAction extends CommonAction {
public function index(){
import('ORG.Util.Page');
//import调用的是message/ThinkPHP框架目录下的扩展包Extend/Library/ORG/Util/中的Page.class.php类文件
$count = M('board')->count();
//调用board库,取出所有数据条数
$page = new Page($count ,10);
//实例化Page类,其中第一个参数为显示条数的总数,每次取出十条,也就是下面$page->listRows的值
$limit = $page->firstRow . ',' . $page->listRows;
//$page->firstRow为查找的起始条数,默认为0,如果$page->listRows为10,那么第2页的$page->firstRow就为10,以此类推
$board = M('board')->order('time DESC')->limit($limit)->select();
//注意,这里较之前的版本添加了->limit($limit)
$this->board = $board;
$this->page = $page->show();
//将$page->show()通过show方法解析$page内容显示并赋值给模板变量,供模板调用
$this->display();
}
Public function delete(){
$id = I('id','','intval');
if(M('board')->delete($id)){
$this->success('删除成功',U('index'));
}else{
$this->error('删除失败');
}
}
}
show方法是3.1版本才有的一个新功能
ThinkPHP中页面输出的过程是读取模板文件,然后进行模板解析(也支持调用第三方模板引擎解析),但是有一些情况,我们并没有定义模板文件,或者把模板文件保存在数据库里面,那么这个时候进行页面输出的时候,我们是无法进行模板文件读取的,3.1版本则针对这样的情况增加了内容解析输出的功能。
内置的模板引擎也进行了完善,如果传入的模板文件不存在的话,则会认为是传入的模板解析内容,因此,3.1的View类和Action类也做了一些改进。
display方法用于模板文件渲染输出,show方法则用于模板内容渲染输出,并且show方法仍然支持内容解析功能
具体内容可参考:ThinkPHP3.1新特性 内容解析输出
二、在模板文件中加入分页模块
知识点:
1、td单元格合并
2、$page变量调用显示
编辑文件:admin/Tpl/MsgManage/index.html,加入一段tr用来显示分页相关,代码如下:
复制代码代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Message Board BackGround</title>
</head>
<body>
<table class="table" border="1">
<tr>
<th>ID</th>
<th>发布者</th>
<th>内容</th>
<th>发布时间</th>
<th>操作</th>
</tr>
<foreach name='board' item='b'>
<tr>
<td>{$b.id}</td>
<td>{$b.username}</td>
<td>{$b.content}</td>
<td>{$b.time|date='y-m-d H:i',###}</td>
<td><a href="{:U('admin.php/MsgManage/delete',array('id' => $b['id'])),''}">删除</a></td>
</tr>
</foreach>
//新增tr代码短
<tr>
<td colspan='5' align='center'>
//将5个单元格合并,并且居中显示
{$page}
//显示控制器中$this->page内容
</td>
</tr>
</table>
</body>
</html>
展开全部
用php的数组切分函数进行分页,array_slice(array,offset,length,preserve)如果使用这个进行分页的话要考虑性能问题,这个要把全部数据读出来之后分页跟,在数据库中使用limit不一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
tp不是有个分页类么,结合mysql的limit
import('util.Page');
$page=new Page($count,10);//$count是查询出来的总记录数
$sql="SELECT photo,userid,relname,username,Phone,qq, b.name AS Sex,c.name AS Age,d.name AS marriage,
e.name AS height,f.name AS weight,g.name AS school,h.name AS salary,i.name AS house,
j.name AS profession,k.name AS zage1,l.name AS zage2,m.name AS Zheight1,n.name AS Zheight2,
o.name AS zschool,p.name AS zmoney,r.name AS place
FROM ty_userinfo a
LEFT JOIN ty_class2 b ON a.Sex=b.id
LEFT JOIN ty_class2 c ON a.Age=c.id
LEFT JOIN ty_class2 d ON a.marriage=d.id
LEFT JOIN ty_class2 e ON a.height=e.id
LEFT JOIN ty_class2 f ON a.weight=f.id
LEFT JOIN ty_class2 g ON a.school=g.id
LEFT JOIN ty_class2 h ON a.salary=h.id
LEFT JOIN ty_class2 i ON a.house=i.id
LEFT JOIN ty_class2 j ON a.profession=j.id
LEFT JOIN ty_class2 k ON a.zage1=k.id
LEFT JOIN ty_class2 l ON a.zage2=l.id
LEFT JOIN ty_class2 m ON a.Zheight1=m.id
LEFT JOIN ty_class2 n ON a.Zheight2=n.id
LEFT JOIN ty_class2 o ON a.zschool=o.id
LEFT JOIN ty_class2 p ON a.zmoney=p.id
LEFT JOIN ty_area r ON a.place=r.id
WHERE (1=1) ";
if(!empty($sex))
{
$sql .=" and Sex= ".$sex;
}
if(!empty($marriage))
{
$sql .=" and marriage = ".$marriage;
}
if (!empty($age1)) {
$sql .=" and Age >= ".$age1;
}
if (!empty($age2)) {
$sql .=" and Age <= ".$age2;
}
if (!empty($place)) {
$sql .=" and place = ".$place[1];
}
$sql.=" limit ".$page->firstRow.','.$page->listRows;
$info=M()->query($sql);
$show=$page->show();//分页
import('util.Page');
$page=new Page($count,10);//$count是查询出来的总记录数
$sql="SELECT photo,userid,relname,username,Phone,qq, b.name AS Sex,c.name AS Age,d.name AS marriage,
e.name AS height,f.name AS weight,g.name AS school,h.name AS salary,i.name AS house,
j.name AS profession,k.name AS zage1,l.name AS zage2,m.name AS Zheight1,n.name AS Zheight2,
o.name AS zschool,p.name AS zmoney,r.name AS place
FROM ty_userinfo a
LEFT JOIN ty_class2 b ON a.Sex=b.id
LEFT JOIN ty_class2 c ON a.Age=c.id
LEFT JOIN ty_class2 d ON a.marriage=d.id
LEFT JOIN ty_class2 e ON a.height=e.id
LEFT JOIN ty_class2 f ON a.weight=f.id
LEFT JOIN ty_class2 g ON a.school=g.id
LEFT JOIN ty_class2 h ON a.salary=h.id
LEFT JOIN ty_class2 i ON a.house=i.id
LEFT JOIN ty_class2 j ON a.profession=j.id
LEFT JOIN ty_class2 k ON a.zage1=k.id
LEFT JOIN ty_class2 l ON a.zage2=l.id
LEFT JOIN ty_class2 m ON a.Zheight1=m.id
LEFT JOIN ty_class2 n ON a.Zheight2=n.id
LEFT JOIN ty_class2 o ON a.zschool=o.id
LEFT JOIN ty_class2 p ON a.zmoney=p.id
LEFT JOIN ty_area r ON a.place=r.id
WHERE (1=1) ";
if(!empty($sex))
{
$sql .=" and Sex= ".$sex;
}
if(!empty($marriage))
{
$sql .=" and marriage = ".$marriage;
}
if (!empty($age1)) {
$sql .=" and Age >= ".$age1;
}
if (!empty($age2)) {
$sql .=" and Age <= ".$age2;
}
if (!empty($place)) {
$sql .=" and place = ".$place[1];
}
$sql.=" limit ".$page->firstRow.','.$page->listRows;
$info=M()->query($sql);
$show=$page->show();//分页
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
该怎么分怎么分啊 分页是对结果集分 跟查询条件多少有什么关系?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询