zoukankan      html  css  js  c++  java
  • phpcmsv9多表联合查询分页功能实现

    phpcms v9里面自带的listinfo分页函数蛮好用的,可惜啊。不支持多表查询并分页。

    看了一下前台模板层支持get标签,支持多表查询,支持分页。刚好可以把这个功能搬到后台来使用。

    我们现在对get_model.class.php进行改造使他能支持多表查询并分页,分享给大家

    <?php
    defined('IN_PHPCMS') or exit('No permission resources.');
    pc_base::load_sys_class('model', '', 0);
    class get_model extends model {
        public $db_config, $db_setting;
        public function __construct($db_config = array(), $db_setting = '') {
            if (!$db_config) {
                $this->db_config = pc_base::load_config('database');
            } else {
                $this->db_config = $db_config;
            }
            if (!$db_setting) {
                $this->db_setting = 'default';
            } else {
                $this->db_setting = $db_setting;
            }
            
            parent::__construct();
            if ($db_setting && $db_config[$db_setting]['db_tablepre']) {
                $this->db_tablepre = $db_config[$db_setting]['db_tablepre'];
            }
        }
        
        public function sql_query($sql) {
            if (!empty($this->db_tablepre)) $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
            return parent::query($sql);
        }
        
        public function fetch_next() {
            return $this->db->fetch_next();
        }
        //自定义分页查询{支持多表}
        public function multi_listinfo($where = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array()) {
            $sql = preg_replace('/select([^from].*)from/i', "SELECT COUNT(*) as count FROM ", $where);
            $this->sql_query($sql);
            $c = $this->fetch_next();
            $this->number = $c['count'];
            $page = max(intval($page), 1);
            $offset = $pagesize*($page-1);
            $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages);
            
            $r = $this->sql_query($where.' LIMIT '.$offset.','.$pagesize);
            while(($s = $this->fetch_next()) != false){
                $data[] = $s;
            }
            return $data;
        }
    }
    ?>

    使用方法和listinfo一样:

    $this->get_db = pc_base::load_model('get_model');
    $page = intval($_GET['page']);
    $infos = $this->get_db->muti_listinfo($where,$page);
    $pages = $this->get_db->pages;

    本文转自:http://www.chanyinkeji.com/phpcms-jiaocheng/10.html

  • 相关阅读:
    16、cgminer学习之:popen函数和system函数详解(执行系统命令)
    16、cgminer学习之:pthread_mutex_init和pthread_cond_init
    15、python学习手册之:元组、文件及其他
    15、python学习手册之:列表和字典
    Chorme浏览器中使用flash debug版本
    Best Practices
    Titled-Contributing to Tiled
    Titled
    VS2010配合SVN搭建使用
    Google code问题记录
  • 原文地址:https://www.cnblogs.com/mengdejun/p/3824854.html
Copyright © 2011-2022 走看看