http://www.thinkphp.cn/extend/538.html
用ThinkPHP的标签做网站觉得不够快速,就自己写了一个扩展,感觉挺好的,分享出来,给有需要的TPER。
复制代码
-
<?php
-
class TagLibEP extends TagLib{
-
protected $tags = array('select'=>array('attr'=>'table,where,order,limit,id,page,sql,field,key,mod,debug','level'=>3));
-
public function _select($attr,$content){
-
$tag = $this->parseXmlAttr($attr,'select');
-
$table =!empty($tag['table'])?$tag['table']:'';
-
$order =!empty($tag['order'])?$tag['order']:'';
-
$limit =!empty($tag['limit'])?intval($tag['limit']):'';
-
$id =!empty($tag['id'])?$tag['id']:'r';
-
$where =!empty($tag['where'])?$tag['where']:' 1 ';
-
$key =!empty($tag['key'])?$tag['key']:'i';
-
$mod =!empty($tag['mod'])?$tag['mod']:'2';
-
$page =!empty($tag['page'])?$tag['page']:false;
-
$sql =!empty($tag['sql'])?$tag['sql']:'';
-
$field =!empty($tag['field'])?$tag['field']:'';
-
$debug =!empty($tag['debug'])?$tag['debug']:false;
-
$this->comparison['noteq']= '<>';
-
$this->comparison['sqleq']= '=';
-
$where =$this->parseCondition($where);
-
$sql =$this->parseCondition($sql);
-
$parsestr.='<?php $m=M("'.$table.'");';
-
-
if($sql){
-
if($page){
-
$limit=$limit?$limit:10;//如果有page,没有输入limit则默认为10
-
$parsestr.='import("@.ORG.Page");';
-
$parsestr.='$count=count($m->query("'.$sql.'"));';
-
$parsestr.='$p = new Page ( $count, '.$limit.' );';
-
$parsestr.='$sql.="'.$sql.'";';
-
-
$parsestr.='$sql.=" limit ".$p->firstRow.",".$p->listRows."";';
-
$parsestr.='$ret=$m->query($sql);';
-
$parsestr.='$pages=$p->show();';
-
//$parsestr.='dump($count);dump($sql);';
-
}else{
-
$sql.=$limit?(' limit '.$limit):'';
-
$parsestr.='$ret=$m->query("'.$sql.'");';
-
}
-
}else{
-
if($page){
-
$limit=$limit?$limit:10;//如果有page,没有输入limit则默认为10
-
$parsestr.='import("@.ORG.Page");';
-
$parsestr.='$count=$m->where("'.$where.'")->count();';
-
$parsestr.='$p = new Page ( $count, '.$limit.' );';
-
$parsestr.='$ret=$m->field("'.$field.'")->where("'.$where.'")->limit($p->firstRow.",".$p->listRows)->order("'.$order.'")->select();';
-
$parsestr.='$pages=$p->show();';
-
}else{
-
$parsestr.='$ret=$m->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$limit.'")->select();';
-
}
-
}
-
if($debug!=false){
-
$parsestr.='dump($ret);dump($m->getLastSql());';
-
}
-
$parsestr.= 'if ($ret): $'.$key.'=0;';
-
$parsestr.= 'foreach($ret as $key=>$'.$id.'):';
-
$parsestr.= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>';
-
$parsestr.= $this->tpl->parse($content);
-
$parsestr.= '<?php endforeach;endif;?>';
-
return $parsestr;
-
-
}
-
}
-
?>
DEMO:
文件放在ThinkPHP\Extend\Driver\TagLib\下,然后在config.php文件中引入,'TAGLIB_PRE_LOAD' => 'EP',
table:表名,where查询条件,order排序,id结果变量,page分页开启,field字段,key循环变量,mod取模,debug是否调试
- <EP:select sql="select * from table order by id desc" limit="10" page="1"></EP:select>
- <EP:select table="article" limit="10"></EP:select>
复制代码
无论是频道页面还是列表页面都可以实现分页了。
-
- <div id="pages">{$pages}</div>
评论(0)