自定义标签无法分页问题对于很多朋友来讲是一个无法解决的问题了,但今天小编看到一篇比较好的处理办法,下面整理给各位参考。
PHPCMS提供了很多标签方便调用数据,但是在二次开发中,有时要自己写PHPCMS标签,但是遇到分页的时候无法显示分页,本人介绍开发PHPCMS标签分页的正确解决方案。
比如PHPCMS的lists标签,代码如下:
{pc:content action=”lists” catid=”2″ where=”thumb!=” AND status=99″ order=”id DESC” num=”4″}
通过自带的lists标签,可以很方便调用某个类别的列表,在模板中调用,显示分页直接用{$pages}即可,在/modules/content/classes/content_tag.class.php中自定义了一个PHPCMS列表标签,但是自定义的列表标签,调用{$pages},分页是出不来的,那么如何进行分页呢?代码如下:
- if (isset($page)) {
- $str .= '$pagesize = '.$num.';';
- $str .= '$page = intval('.$page.') ? intval('.$page.') : 1;if($page<=0){$page=1;}';
- $str .= '$offset = ($page - 1) * $pagesize;';
- $datas['limit'] = '$offset.",".$pagesize';
- $datas['action'] = $action;
- $str .= '$'.$op.'_total = $'.$op.'_tag->count('.self::arr_to_html($datas).');';
- $str .= '$pages = pages($'.$op.'_total, $page, $pagesize, $urlrule);';
- }
我们要了解PHPCMS标签的原理就明白了,在/libs/classes/template_cache.class.php中定义了模板标签解析类,原来lists标签能够分页,调用了pages函数,我们注意第8行,有个_tag->count()的方法,这个方法返回的参数作为pages的第一个参数,也就是总页数,而自定义列表标签,并没有count这个方法.
在/modules/content/classes/content_tag.class.php中,有个count方法,代码如下:
- /**
- * 分页统计
- * @param $data
- */
- public function count($data) {
- if($data['action'] == 'lists') {
- $catid = intval($data['catid']);
- if(!$this->set_modelid($catid)) return false;
- if(isset($data['where'])) {
- $sql = $data['where'];
- } else { //开源软件:Vevb.com
- if($this->category[$catid]['child']) {
- $catids_str = $this->category[$catid]['arrchildid'];
- $pos = strpos($catids_str,',')+1;
- $catids_str = substr($catids_str, $pos);
- $sql = "status=99 AND catid IN ($catids_str)";
- } else {
- $sql = "status=99 AND catid='$catid'";
- }
- }
- return $this->db->count($sql);
- }
看到第6行,当标签动作是lists时,进行一系列操作获取总数,因此我们自定义的列表标签,也应该加一个这样的判断,当$data[‘action’]==’自定义的动作名称’时,计算查询条件的总数,代码如下:
- elseif ($data['action'] == 'c_lists') {
- list($catid,$language,$size,$page) = explode('-', $data['search']);
- //$catid = intval($data['catid']);
- $catid = $catid+1;
- $language_setting = getcache('language_setting', 'commons');
- $size_setting = array(
- 1=>' unit="MB" and size<=10',
- ' unit="MB" and size>=10 and size<=50',
- ' unit="MB" and size>=50 and size<=100',
- ' unit="MB" and size>=100 and size<=200',
- ' unit="MB" and size>=200 and size<=500',
- ' unit="MB" and size>=500 and size<1000',
- ' unit="GB" and size>=1 and size<=2',
- ' unit="GB" and size>=2 and size<=4',
- ' unit="GB" and size>=4',
- );
- if(!$this->set_modelid($catid)) return false;
- if(isset($data['where'])) {
- $sql = $data['where'];
- } else {
- $thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
- if($this->category[$catid]['child']) {
- $catids_str = $this->category[$catid]['arrchildid'];
- $pos = strpos($catids_str,',')+1;
- $catids_str = substr($catids_str, $pos);
- $sql = "status=99 AND catid IN ($catids_str)".$thumb;
- } else {
- $sql = "status=99 AND catid='$catid'".$thumb;
- }
- if ($language) {
- $sql .= " AND language = '".$language_setting[$language]."'";
- }
- if ($size) {
- $sql .= " AND".$size_setting[$size];
- }
- }
- return $this->db->count($sql);
- }
代码紧接着上面的,这样,PHPCMS自定义的列表标签也可以使用{$pages}正常显示分页.
PHPCMS不修改程序自定义分页格式,请看源代码:
- {if $pages}
- <div class="pages">
- <select name="select_pages" onchange="location.href=this.options[this.selectedIndex].value;">
- {str_replace("a href", "option value", str_replace("..", "", str_replace("</a", "</option", str_replace("a class", "option class", str_replace("</span>", "页</option>", str_replace("<span>", "<option selected>第", str_replace("> ", ">", $pages)))))))}</select>{/pc}
- </div>
- {/if}
这里用到了PHP的替换函数str_replace,可以任意的替换默认生成代码,从而实现PHPCMS不修改程序自定义分页格式.
新闻热点
疑难解答