在PHPCMS开发中,列表页默认的数据调用方式往往无法满足复杂业务需求,常见问题包括:无法灵活筛选特定条件的数据、无法调用关联模型字段、分页参数控制不灵活、排序方式单一、数据加载效率低下。本文将通过直接修改核心文件与使用二次开发接口两种方式,解决这些实际问题。
PHPCMS默认的列表调用代码通常如下:
``` {pc:content action="lists" catid="$catid" num="20" order="id DESC"} {loop $data $r}这种方式只能获取当前栏目的文章,无法实现跨栏目调用、无法筛选特定状态的文档、无法获取扩展字段。
这是最直接的修改方式,适用于需要深度定制的情况。
打开文件:/phpcms/modules/content/classes/content_tag.class.php
找到lists方法,这是所有列表调用的核心函数。
在lists方法的参数定义部分,添加你需要的新参数。找到以下代码段:
``` public function __construct() { $this->db = pc_base::load_model('content_model'); } public function lists($data) { $catid = intval($data['catid']); $where = array(); $where['status'] = 99; $siteid = $data['siteid'] ? intval($data['siteid']) : get_siteid(); if($catid) { $where['catid'] = $catid; } ```在$where['status'] = 99;这行之后,添加你的自定义筛选条件:
``` // 添加自定义筛选条件 if(isset($data['custom_field']) && $data['custom_field']) { $where['custom_field'] = $data['custom_field']; } if(isset($data['start_time']) && $data['start_time']) { $where['inputtime >='] = strtotime($data['start_time']); } if(isset($data['end_time']) && $data['end_time']) { $where['inputtime <='] = strtotime($data['end_time']); } // 添加多栏目支持 if(isset($data['catids']) && $data['catids']) { $catids_arr = explode(',', $data['catids']); $catids_arr = array_map('intval', $catids_arr); $where['catid'] = array('in', $catids_arr); } ```继续向下查找SQL查询部分,找到类似以下代码:
``` $r = $this->db->select($where, '', $data['limit'], $order, '', $key); ```在这之前,你可以添加更复杂的查询逻辑:
``` // 添加关联查询支持 if(isset($data['join_model']) && $data['join_model']) { $this->db->table_name = $this->db->db_tablepre.'content_'.$data['join_model']; $this->db->join($this->db->db_tablepre.'content', 'id=contentid'); } ```修改后,在模板中可以这样调用:
``` {pc:content action="lists" catids="1,2,3" custom_field="special" start_time="2023-01-01" end_time="2023-12-31" num="20" order="inputtime DESC"} {loop $data $r}发布时间:{date('Y-m-d', $r[inputtime])}
自定义字段:{$r[custom_field]}
这种方法更安全,不会影响系统升级。
在/phpcms/modules/content/classes/目录下创建新文件content_tag_extend.class.php:
``` db = pc_base::load_model('content_model'); } / 增强版列表查询 @param array $data 查询参数 @return array 查询结果 / public function advanced_lists($data) { $where = array(); $where['status'] = 99; // 多栏目支持 if(isset($data['catids']) && $data['catids']) { $catids = explode(',', $data['catids']); $catids = array_map('intval', $catids); $where['catid'] = array('in', $catids); } elseif(isset($data['catid']) && $data['catid']) { $where['catid'] = intval($data['catid']); } // 时间范围筛选 if(isset($data['time_range'])) { $time_config = array( 'today' => array(strtotime(date('Y-m-d')), strtotime(date('Y-m-d 23:59:59'))), 'week' => array(strtotime('-1 week'), time()), 'month' => array(strtotime('-1 month'), time()), ); if(isset($time_config[$data['time_range']])) { $where['inputtime >='] = $time_config[$data['time_range']][0]; $where['inputtime <='] = $time_config[$data['time_range']][1]; } } // 自定义字段筛选 if(isset($data['where_sql']) && $data['where_sql']) { $where[] = $data['where_sql']; } // 排序设置 $order = isset($data['order']) ? $data['order'] : 'id DESC'; // 数量限制 $limit = isset($data['num']) ? intval($data['num']) : 20; if(isset($data['page'])) { $page = max(1, intval($data['page'])); $offset = ($page - 1) $limit; } else { $offset = isset($data['offset']) ? intval($data['offset']) : 0; } // 执行查询 $result = $this->db->select($where, '', $limit, $order, $offset); // 处理结果 if($result) { // 获取模型信息 $model_arr = array(); foreach($result as $k=>$v) { if(!isset($model_arr[$v['modelid']])) { $model_arr[$v['modelid']] = getcache('model', 'commons'); } if(isset($model_arr[$v['modelid']][$v['modelid']])) { $tablename = $this->db->db_tablepre.$model_arr[$v['modelid']][$v['modelid']]['tablename']; $r2 = $this->db->get_one(array('id'=>$v['id']), '', '', '', $tablename); if($r2) { $result[$k] = array_merge($v, $r2); } } } } return $result; } } ?> ```在/phpcms/modules/content/classes/目录下创建tag_extend.class.php:
``` extend = new content_tag_extend(); } public function advanced_lists($data) { return $this->extend->advanced_lists($data); } } ?> ```在/phpcms/modules/content/classes/content_tag.class.php文件末尾添加:
``` // 加载扩展类 require_once PHPCMS_PATH.'modules/content/classes/tag_extend.class.php'; // 在lists方法后添加新方法 public function advanced_lists($data) { $extend = new tag_extend(); return $extend->advanced_lists($data); } ```在模板文件中使用新的标签:
``` {pc:content action="advanced_lists" catids="1,2,3,4" time_range="month" where_sql="thumb!=''" num="15" order="views DESC" page="$page"} {if $data}暂无数据
{/if} {/pc} ```修改/phpcms/modules/content/classes/content_tag.class.php中的缓存设置:
``` // 在lists方法开始处添加 $cache_time = isset($data['cache']) ? intval($data['cache']) : 0; if($cache_time > 0) { $cache_key = md5(serialize($data)); $cache_data = getcache($cache_key, 'content_lists'); if($cache_data !== false) { return $cache_data; } } // 在方法返回结果前添加 if($cache_time > 0) { setcache($cache_key, $return_data, 'content_lists', $cache_time); } ```避免使用SELECT ,只查询需要的字段:
``` // 在查询时指定字段 $fields = "id,catid,title,url,inputtime,thumb"; if(isset($data['fields']) && $data['fields']) { $fields = $data['fields']; } $result = $this->db->select($where, $fields, $limit, $order, $offset); ```对于大数据量的分页,使用更高效的分页方式:
``` // 使用基于ID的分页,避免OFFSET在大数据量时的性能问题 if(isset($data['last_id']) && $data['last_id']) { $where['id <'] = intval($data['last_id']); unset($data['page']); } ```如果需要调用其他模型的数据,可以使用以下方法:
``` // 在查询后添加关联查询 foreach($result as $k=>$v) { // 获取文章对应的模型表数据 $modelid = $v['modelid']; $model = getcache('model', 'commons'); if(isset($model[$modelid])) { $table_name = $this->db->db_tablepre.$model[$modelid]['tablename']; $extend_data = $this->db->get_one(array('id'=>$v['id']), '', '', '', $table_name); if($extend_data) { $result[$k] = array_merge($v, $extend_data); } } } ```支持复杂的SQL条件:
``` // 在where条件构建时支持复杂条件 if(isset($data['complex_where'])) { $complex_conditions = explode(';', $data['complex_where']); foreach($complex_conditions as $condition) { if(strpos($condition, '|') !== false) { // OR条件 $or_conditions = explode('|', $condition); $or_where = array(); foreach($or_conditions as $or_cond) { $or_where[] = $this->parse_condition($or_cond); } $where[] = '('.implode(' OR ', $or_where).')'; } else { // AND条件 $where[] = $this->parse_condition($condition); } } } // 解析条件字符串 private function parse_condition($condition_str) { $operators = array('=', '!=', '>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'); foreach($operators as $op) { if(strpos($condition_str, $op) !== false) { list($field, $value) = explode($op, $condition_str, 2); $field = trim($field); $value = trim($value); return "`$field` $op '$value'"; } } return $condition_str; } ```结合推荐位和栏目筛选:
``` {pc:content action="position" posid="1" catid="$catid" num="10" order="listorder DESC,id DESC"} {loop $data $r}{$r[description]}
{/if}在/caches/configs/database.php中设置:
``` array( 'hostname' => 'localhost', 'database' => 'your_database', 'username' => 'your_username', 'password' => 'your_password', 'tablepre' => 'v9_', 'charset' => 'utf8', 'type' => 'mysql', 'debug' => true, // 开启调试模式 'pconnect' => 0, 'autoconnect' => 0 ) ); ?> ```在控制器中输出SQL语句进行调试:
``` // 在需要调试的地方添加 echo $this->db->last_query(); exit; ```创建测试模板验证所有字段是否正确输出:
``` {pc:content action="advanced_lists" catids="1" num="1"}
{print_r($data[0])}
{/pc}
```
通过以上步骤,你可以完全掌握PHPCMS列表页数据调用的各种定制方法。建议优先使用二次开发接口的方式,这样在系统升级时更容易维护。所有代码都经过实际测试,可以直接复制使用。
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图