限制和偏移量在使用Codeigniter的主页中不起作用

时间:2019-03-19 13:02:47

标签: php codeigniter

在主页中,限制和偏移不起作用。在其他页面上使用下面的相同代码可以正常工作。请任何人在代码上帮助我。

主页上正在调用多个不同的模型。

它显示所有记录,尽管我将$ config [per_page]设置为2。

控制器:

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pages extends CI_Controller {

    public function view($page='home',$offset=0)
    {
        if(!file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
            show_404();
        }
        $data['meta_description']="state science warangal";
        $data['meta_keywords']="state, science, warangal";
        $data['title']=ucfirst($page);
        $data['important'] = $this->article_model->get_articles_by_category();

        $this->load->library('pagination');

      //pagination config
        $config['base_url'] = base_url() . '/';
        $config['total_rows'] = $this->db->count_all('articles');
        $config['per_page'] = 2;
        $config['uri_segment'] = 1;
        $config['attributes'] = array('class' => 'pagination-links');
        //init pagination
        $this->pagination->initialize($config);
        $config['use_page_numbers'] = TRUE;
        $data['latest'] = $this->article_model->get_posts(FALSE, $config['per_page'],$offset); 
        $this->load->view('templates/header',$data);
        $this->load->view('pages/'.$page,$data);
        $this->load->view('templates/footer');
    }    
}

以下是模型:

    public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE) {
        if ($limit != '' && $offset != '') {
            $this->db->limit($limit, $offset);
        }
        if ($slug === FALSE) {

            $this->db->select('articles.*,GROUP_CONCAT(categories.name  SEPARATOR "," ) as name');

            $this->db->join('articlecategories', 'articlecategories.article_id = articles.article_id');
            $this->db->join('categories', 'articlecategories.category_id = categories.id');

            $this->db->group_by('articlecategories.article_id', 'articlecategories.id');
            $this->db->order_by('articles.article_id', 'DESC');
            $query = $this->db->get_where('articles', array('articles.slug' => $slug));

            return $query->result_array();
        }
        $this->db->select('articles.*, GROUP_CONCAT(categories.name  SEPARATOR "," ) as name');

        $this->db->join('articlecategories', 'articlecategories.article_id = articles.article_id');
        $this->db->join('categories', 'articlecategories.category_id = categories.id');

        $this->db->group_by('articlecategories.article_id');
        $this->db->order_by('articles.title', 'DESC');
        $this->db->order_by('articles.article_id', 'DESC');
        $query = $this->db->get_where('articles', array('articles.slug' => $slug));
        return $query->row_array();
    }

0 个答案:

没有答案