2012-02-11 83 views
1

我有分頁類笨 這裏的一個小問題是我的控制器分頁使用笨

function index() 
    { 
     $this->load->library('pagination'); 
     $config['base_url'] = base_url().'index.php/books/index/'; 
     $config['total_rows'] = $this->db->count_all('books'); 
     $config['per_page'] = '5'; 
     $config['full_tag_open'] = '<p>'; 
     $config['full_tag_close'] = '</p>'; 
     $this->pagination->initialize($config); 
     echo "Here"; 
     //load the model and get results 
     $this->load->model('books_model'); 
     $data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3)); 

     // load the HTML Table Class 
     $this->load->library('table'); 
     $this->table->set_heading('Title'); 

     // load the view 
     $this->load->view('books_view', $data); 
    } 

,這裏是視圖

<body> 
<h1>Books</h1> 
<?php echo $this->table->generate($results); ?> 
<?php echo $this->pagination->create_links(); ?> 
</body> 

和模型

function get_books($num, $offset) { 
    $query = $this->db->get('books', $num, $offset);  
    return $query; 
    } 

我設置了我的數據庫,並且已經路由(default-> books),但是頁面沒有顯示出來nything。我的數據庫表是書。

+2

你確定你從數據庫中獲得結果嗎?如果你沒有得到任何結果,分頁不會顯示 – Henesnarfel 2012-02-13 14:24:11

回答

0

我不知道$config是你自己班的變量。如果它是全局配置,也許你應該使用$this->config->set_item函數來設置它的參數。

此外,爲避免像前面提到的@Henesnarfel這樣的麻煩,嘗試在執行查詢時回顯結果數量。

function index() 
{ 
    $this->load->library('pagination'); 
    $this->config->set_item('base_url',base_url().'index.php/books/index/'); 
    $this->config->set_item('total_rows',$this->db->count_all('books')); 

    // ECHO to make me sure that the query is succesfully done 
    echo("Found ".$this->db->count_all('books')." books"); 

    $this->config->set_item('per_page','5'); 
    $this->config->set_item('full_tag_open','<p>'); 
    $this->config->set_item('full_tag_close','</p>'); 
    $this->pagination->initialize($config); 
    echo "Here"; 

    //load the model and get results 
    $this->load->model('books_model'); 
    $data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3)); 

    // load the HTML Table Class 
    $this->load->library('table'); 
    $this->table->set_heading('Title'); 

    // load the view 
    $this->load->view('books_view', $data); 
}