CodeIgniter分页。不确定如何实施

时间:2013-09-19 08:00:25

标签: php codeigniter pagination

我无法实现CI基本安装附带的CodeIgniter分页类。

我有一个页面显示新闻文章并在页面上显示。我希望每页最多显示5篇文章,并且可以浏览这些页面。

我已阅读CI文档,但我不确定我应该将config ['base_url']变量指向。这是我的代码(请记住我已从示例中删除了与此问题无关的代码):

控制器:

class News extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
//Get News Articles
        $options = array(
            'orderBy' => 'DESC'
        );
        $newsArticles = $this->admin_model->getNewsArticles($options);
        $data['newsArticles'] = array();
        foreach($newsArticles as $newsArticle) { 
            $date = date('F j, Y, g:i a', strtotime($newsArticle['date_created']));
            $active = ($newsArticle['active'] == 1 ? 'tick.png' : 'cross.png');
            $data['newsArticles'][] = array(
                'articleId'  =>  $newsArticle['news_id'],
                'title'      =>  $newsArticle['title'],
                'content'    =>  htmlspecialchars_decode(stripslashes($newsArticle['content'])),
                'date'       =>  $date,
                'author'     =>  $newsArticle['author'],
                'active'     =>  $this->config->base_url() . "/images/" . $active,
                'link'       =>  $this->config->base_url() . "/index.php/admin/newsPage?articleId=" . $newsArticle['news_id']
            );
        }

        //Pagination

        $config['base_url'] = $this->config->base_url() . "index.php/news/getNewsArticles";
        $config['total_rows'] = 200;
        $config['per_page'] = 20; 

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

模型函数只是一个返回新闻文章的函数,在这里工作正常。

我是否需要将base_url变量指向一个将数据库结果直接返回到分页类的函数?我不确定它是如何实施的。

如果有人能指出我正确的方向,或建议我哪里出错,那就太棒了。

2 个答案:

答案 0 :(得分:1)

好的,我在检查了Ellislab教程后设法让它工作了。这是修改后的控制器。请注意,在我的模型函数中,我还添加了一个子句来捕获偏移量。

//Pagination
        $config['base_url']   = $this->config->base_url() . "index.php/news/news/index";
        $config['total_rows'] = $this->admin_model->getMaxNewsRows();
        $config['per_page']   = 5;
        $config['uri_segment'] = 4;
        $config['num_links']  = 5; 
        $config['next_link']        = 'Next';
        $config['next_tag_open']    = '<span class="nextPage">';
        $config['next_tag_close']   = '</span>';
        $config['prev_link']        = 'Prev';
        $config['prev_tag_open']    = '<span class="prevPage">';
        $config['prev_tag_close']   = '</span>';
        $config['cur_tag_open']     = '<span class="active_page">';
        $config['cur_tag_close']    = '</span>';

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

        //Get News Articles
        $offset = $this->uri->segment(4);

        $options = array(
            'orderBy' => 'DESC',
            'limit' => $config['per_page']
        );
        if($offset != 0) {
            $options['offset'] = $offset;
        }
        $newsArticles = $this->admin_model->getNewsArticles($options);
        //$newsArticles = $this->db->get('news', $config['per_page'], $this->uri->segment(4));

        $data['newsArticles'] = array();
        foreach($newsArticles as $newsArticle) { 
            $date = date('F j, Y, g:i a', strtotime($newsArticle['date_created']));
            $active = ($newsArticle['active'] == 1 ? 'tick.png' : 'cross.png');
            $data['newsArticles'][] = array(
                'articleId'  =>  $newsArticle['news_id'],
                'title'      =>  $newsArticle['title'],
                'content'    =>  htmlspecialchars_decode(stripslashes($newsArticle['content'])),
                'date'       =>  $date,
                'author'     =>  $newsArticle['author'],
                'active'     =>  $this->config->base_url() . "/images/" . $active,
                'link'       =>  $this->config->base_url() . "/index.php/admin/newsPage?articleId=" . $newsArticle['news_id']
            );
        }       
        $data['pagLinks'] =  $this->pagination->create_links(); 

答案 1 :(得分:0)

试试这个:

    public function index() {
//Get News Articles
$options = array(
    'orderBy' => 'DESC'
);

#pagination start
$perPage                    = 20;   #rows per page 
$this->load->library('pagination');
$config['base_url']         = base_url().'index.php/news/getNewsArticles/';
$config['total_rows']       = $this->admin_model->count_getNewsArticles( $options ); #should return the total rows in integer
$config['per_page']         = $perPage;
$config["uri_segment"]      = 3;
$config['next_link']        = 'Next';
$config['next_tag_open']    = '<span class="nextPage">';
$config['next_tag_close']   = '</span>';
$config['prev_link']        = 'Prev';
$config['prev_tag_open']    = '<span class="prevPage">';
$config['prev_tag_close']   = '</span>';
$config['cur_tag_open']     = '<span class="active_page">';
$config['cur_tag_close']    = '</span>';
$this->pagination->initialize($config);
#pagination end
$data['links']              = $this->pagination->create_links();

$newsArticles = $this->admin_model->getNewsArticles($options, $perPage, $this->uri->segment(3));
$data['newsArticles'] = array();
foreach($newsArticles as $newsArticle) { 
    $date = date('F j, Y, g:i a', strtotime($newsArticle['date_created']));
    $active = ($newsArticle['active'] == 1 ? 'tick.png' : 'cross.png');
    $data['newsArticles'][] = array(
        'articleId'  =>  $newsArticle['news_id'],
        'title'      =>  $newsArticle['title'],
        'content'    =>  htmlspecialchars_decode(stripslashes($newsArticle['content'])),
        'date'       =>  $date,
        'author'     =>  $newsArticle['author'],
        'active'     =>  $this->config->base_url() . "/images/" . $active,
        'link'       =>  $this->config->base_url() . "/index.php/admin/newsPage?articleId=" . $newsArticle['news_id']
    );
}


###changes in the model function

function getNewsArticles($options, $perPage, $offset = 0){
    $this->db->get('table_name', $limit, $offset); #in query
}