<?php
class CommonModel extends Model {
public function _list_($map = array() ,$page = 1,$limit = 16, $orderby = "id desc"){
$list['count'] = $this->where($map)->count('id');
$list['page'] = $page;
$list['map'] = $map;
$list['limit'] = $limit;
$pagecount = ceil($list['count'] / $list['limit']);
if ($pagecount < 1) $pagecount =1;
$list['pagecount'] = $pagecount;
$vlist = $this->where($map)->page($page,$limit)->order("$orderby ")->select();
$vvlist = array();
foreach ($vlist as $k) {
if (method_exists($this, 'ckvo')) {
$vvlist[] = $this->ckvo($k);
}else{
$vvlist[] = $k;
}
}
$list['volist'] = $vvlist;
return $list;
}
public function getOne($map='')
{
$vo = $this->where($map)->order('id desc')->find();
if (method_exists($this, 'ckvo')) {
$vo = $this->ckvo($vo);
}
return $vo;
}
}
?>使用了page方法,并且返回了分页所有必须的参数,这里还建议尽量不要使用TP的关联方法,这里我用ckvo方法代替了,public function ckvo($vo='')
{
if (!$vo) return;
$Comment = D('Comment');
$comment = $Comment->getAll($vo['id'],'Info');
if ($comment) {
$vo['Comment'] = $comment;
}
return $vo;
}这样虽然多次查询了,但并不比TP的联合查询性能差,其实最终mysql解析两者原理都一样的,关键是这样我可以任意组合也更方面查询。public function ckvo($vo = '')
{
if (!$vo) return;
$LotteryItem = M('LotteryItem');
$map['lid'] = $vo['id'];
$vlist = $LotteryItem->where($map)->select();
for ($i=0; $i < 6; $i++) {
$vo['Item'][$i] = $vlist[$i];
}
if ($vo['status'] == 0)
$vo['status_text'] = "未开始";
elseif ($vo['status'] == 1)
$vo['status_text'] = "报名中";
elseif ($vo['status'] == 2)
$vo['status_text'] = "抽奖中";
elseif ($vo['status'] == 3)
$vo['status_text'] = "抽奖结束";
return $vo;
}后台请使用普通URL模式ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。