当前位置 : 主页 > 网络编程 > PHP >

CodeIgniter查询分页代码

来源:互联网 收集:自由互联 发布时间:2021-06-30
ci自定义分页功能demo,代码核心来自http://www.cnblogs.com/star-studio/archive/2011/09/27/2193303.html 1. [代码] models public function GetOrderOrigin($options = array()){ // default values $options = $this-_default(array('sor
ci自定义分页功能demo,代码核心来自http://www.cnblogs.com/star-studio/archive/2011/09/27/2193303.html

1. [代码]models    

public function GetOrderOrigin($options = array()){

        // default values
        $options = $this->_default(array('sortDirection' => 'desc'), $options);

        // Add where clauses to query
        $qualificationArray = array('id','orderno','orderdesc','ordertype','userid','addtime','addressid','orderstatus','paymethod','total');

        foreach($qualificationArray as $qualifier)
        {
            if(isset($options[$qualifier]))
                $this->db->where($qualifier, $options[$qualifier]);
        }

        // If limit / offset are declared (usually for pagination)
        // then we need to take them into account
        if(isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        else if(isset($options['limit'])) $this->db->limit($options['limit']);

        // sort
        if(isset($options['sortBy']))
            $this->db->order_by($options['sortBy'], $options['sortDirection']);

        $query = $this->db->get('orderorigin');

        if($query->num_rows() == 0) return false;

        if(isset($options['id']))
        {
            // If we know that we're returning a singular record,
            // then let's just return the object
            return $query->row(0);
        }
        else
        {
            // If we could be returning any number of records
            // then we'll need to do so as an array of objects
            return $query->result();
        }
    }

2. [代码]controller    

function order()
    {
        $offset = $this->uri->segment(3);
        if (!isset($offset)) {
            $offset = 10;
        }
        $_SESSION['offset'] = $offset;
        if ($this->_is_log_in()) {
            $this->header();
            $this->load->model('orderorigin');

            $config['base_url'] = base_url() . 'manager/order/';
            $config['total_rows'] = count($this->orderorigin->GetOrderorigin(array('sortBy' => 'addtime', 'sortDirection' => 'desc')));
            $config['per_page'] = 10;
            $config['full_tag_open'] = '<p>';
            $config['full_tag_close'] = '</p>';
            $config['first_link'] = '首页';
            $config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
            $config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
            $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
            $config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
            $config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
            $config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
            $config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
            $config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
            $config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
            $config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
            $config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
            $config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
            $config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
            $config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
            $config['num_tag_close'] = '</li>';

            $this->pagination->initialize($config);

            $rs = $this->orderorigin->GetOrderorigin(array('sortBy' => 'addtime', 'sortDirection' => 'desc', 'limit' => $config['per_page'], 'offset' => $offset));
            $data['rs'] = $rs;
            $this->load->view("manager_order", $data);
            $this->footer();
        } else {
            $this->login();
        }
    }
网友评论