2

我在实现 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 变量指向一个将数据库结果直接返回到分页类的函数?我不确定它是如何实施的。

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

4

2 回答 2

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(); 
于 2013-09-19T09:20:50.650 回答
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
}
于 2013-09-19T08:08:39.840 回答