0

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

我在数据库中有 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();?>

任何帮助将不胜感激。

问候,佐兰

4

2 回答 2

1

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

最简单的改变是:

    // 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 个时,库认为它实际上是在显示所有结果,因此不需要分页链接!

于 2012-09-19T09:31:27.663 回答
0

像这样编辑

$offset = $this->uri->segment(3) ? $this->uri->segment(3) : 0;
于 2012-09-19T09:27:19.547 回答