【问题标题】:CodeIgniter pagination. Unsure how to implementCodeIgniter 分页。不确定如何实施
【发布时间】:2013-09-24 05:08:53
【问题描述】:

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

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

【问题讨论】:

  • 您好,CI 分页非常棒且易于使用,请先去看一些教程(并非所有教程都只是那些您感兴趣的教程),它的解释很好。这是来源net.tutsplus.com/sessions/codeigniter-from-scratch
  • 非常感谢。我按照本教程设法让它工作。
  • 还请注意$offset 可以是您的控制器方法的最后一个参数;) 像这样public function method($arg1 = '', $arg2 = '', $offset = 0) { },还请注意它不适用于index() 方法,您必须使用_remap()。祝你有美好的一天。
  • 你应该加载 url_helper 而不是到处写 $this->config->base_url,你可以使用 base_url() 代替。并且请不要使用 base_url 进行分页使用 site_url 函数,例如:site_url("news/index")

标签: php codeigniter pagination


【解决方案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(); 

【讨论】:

    【解决方案2】:

    试试这个:

        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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2022-12-22
      • 2013-12-18
      • 2011-07-29
      • 1970-01-01
      • 2020-11-22
      相关资源
      最近更新 更多