分页在codeigniter中不起作用

时间:2012-09-19 09:23:46

标签: codeigniter pagination

我已经成功地在我正在使用的应用程序的某些页面上创建了分页,但是我无法在这个页面上创建:

我在数据库中有7条记录,何时 页面显示所有7条记录显示而不是5,正如我想要的那样。

果然,不显示分页链接。

这是我的控制器代码:

public function displayAllFaqCategories()
    {

         //initializing & configuring paging

        $currentUser = $this->isLoggedIn();
        $this->load->model('faqCategoriesModel');
        $this->db->order_by('sorder');
        $limit = 5;
        $offset = 3;

        $offset = $this->uri->segment(3);
        $this->db->limit(5, $offset);

        $data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
        $totalresults = $this->db->get('faq_categories')->num_rows();

        //initializing & configuring paging
        $this->load->library('pagination');
        $config['base_url'] = site_url('/backOfficeUsers/faqcategories');
        $config['total_rows'] = $totalresults;
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;


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

        $errorMessage = '';
        $data['main_content'] = 'faq/faqcategories';
        $data['title'] = 'FAQ Categories';

        $this->load->vars($data,$errorMessage);
        $this->load->vars($currentUser);
        $this->load->view('backOffice/template');

    } // end of function displayAllFaqCategories

这是我的模型功能代码:

public function selectCategoriesAndParents($selectWhat = array())
   {    
       $data = array();
       $query = $this->db->query("SELECT fq . * , COALESCE( fqp.$this->parent_name,  '0' ) AS parentname
                                  FROM $this->table_name AS fq
                                  LEFT OUTER JOIN $this->table_name AS fqp ON fqp.catid = fq.parentid"); 
       if($query->num_rows() > 0) 
       {
           foreach($query->result_array() as $row) 
               {
                   $data[] = $row;  
               }
           }           
       $query->free_result();
       return $data;
   } // end of function selectCategoriesAndParents    

在视图中,带有记录的表格下面我有以下代码:

 <?php echo $this->pagination->create_links();?>

任何帮助都将深受赞赏。

的问候,卓然

2 个答案:

答案 0 :(得分:1)

我认为你把两种不同的东西混合在了一起。您部分使用ActiveRecord CI,但随后自行运行查询。

最简单的改变是:

    // get all the rows
    $data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
    // figure out the count of all of them
    $totalresults = count($data['faq_categories']);
    // only take some of the rows of the array, instead of keeping all of them and then showing all 7 of your records
    $data['faq_categories'] = array_splice($data['faq_categories'], $offset, $limit);

希望能解决它!

为了进一步解释原始问题是什么,我认为当你运行时:

$totalresults = $this->db->get('faq_categories')->num_rows();

将前一行$this->db->limit(5, $offset);考虑在内,因此它只返回5行。然后,当您告诉分页库您只想每页显示5个时,库会认为它实际上显示了所有结果,因此不需要分页链接!

答案 1 :(得分:0)

像这样编辑

$offset = $this->uri->segment(3) ? $this->uri->segment(3) : 0;