2012-09-19 69 views
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();?> 

任何幫助將深表謝意。

問候,卓然

回答

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個時,圖書館認爲它實際上顯示了所有的結果,所以不需要分頁鏈接!

+0

你搖滾manavo。非常感謝。 – Zoran

0

編輯這樣

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

我改變了這樣的代碼:// $ offset = $ this-> uri-> segment(3); $ offset = $ this-> uri-> segment(3)? $ this-> uri-> segment(3):0;結果保持不變,所有7條記錄都顯示出來,沒有分頁鏈接... – Zoran

+0

NO .. ?? wat是$ Totalpages的值.. ?? – Gautam3164