2011-07-21 62 views
0

我需要幫助,我的代碼點火器app..i似乎無法找出是什麼導致分頁不工作笨:分頁+表庫(沒有工作)

這裏是我的控制器

$limit = 15; 
     $uri_segment = 4; 
     $this->load->helper('string'); 

     $offset = $this->uri->segment($uri_segment); 
     log_message('error',date('Y-m-d H:i:s', time())); 
     //load 
     //$products = $this->Products_model->get_all(); 
     $products = $this->Products_model->get_products($limit, $uri_segment); 
     //var_dump($products); 

     log_message('error', $products); 

     //pagination 
     $this->load->library('pagination'); 
     $config['base_url'] = site_url('admin/products/index/'); 
     $config['total_rows'] = $this->Products_model->count_all(); 
     $config['per_page'] = $limit; 
     $config['uri_segment'] = $uri_segment; 
     $this->pagination->initialize($config); 

     $data['pagination'] = $this->pagination->create_links(); 

     //table 
     $this->load->library('table'); 
     $this->table->set_empty(" "); 
     $this->table->set_heading('ID', 'Product','Category', 'Actions'); 
     $i = 0 + $offset; 
     foreach ($products as $product){ 
      ++$i; 
      $this->table->add_row($product->id, $product->title, $this->Categories_model->get_by_id($product->category_id)->title, 
       anchor('/admin/products/update/'.$product->id,'update',array('class'=>'update')).' '. 
       anchor('/admin/products/delete/'.$product->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this product?')")) 
      ); 
     } 
     $data['table'] = $this->table->generate(); 

     //load template 
     $this->products_list(); 
     $this->products_template(); 
     $this->template->build('products/productList', $data); 

這裏是我的模型

function get_products($num, $offset) { 
    return $this->db->get($this->tbl_products, $num, $offset)->result();  
} 

它生成的鏈接,但那麼它不會加載下一組記錄..任何更正上面的代碼?

謝謝!

+0

我敢肯定分頁庫只是爲您生成的鏈接,它給你傳遞的偏移量和限制模型的get()函數(或者無論你怎麼稱呼它)。 – jessica

+0

確切地說,看看get_where,它有你所需要的。 – Ben

+0

你能給我一個提示或樣品嗎?即時通訊剛剛進入PHP編程 – nhoyti

回答

0

您應該將頁面放在表格中,而不是整個表格。分頁只是填充鏈接,但不會對數據進行任何更改,這是您的責任。

請更改:

 $products = $this->Products_model->get_all(); 

有:

 $products = $this->Products_model->get_page($Offset, $this->limit); // It's not clear where do you define limit. 

,並在模型中做出選擇propper:

function get_page($offset, $limit){ 
    return $this->db->get($this->tbl_products, $limit, $offset)->result(); 
} 
+0

嗨,我已經更新了我的代碼就像你的建議..但然後它不會加載下一組記錄 – nhoyti

+0

請檢查分頁鏈接中的'偏移量'的值。你能給我記錄的數量和'極限'的價值嗎?您在代碼 –

+0

中不清楚您寫了'$ products = $ this-> Products_model-> get_products($ limit,$ uri_segment);',將'$ uri_segment'更改爲'$ offset' –

0

你應該確保你的極限($這個 - >限制)具有良好的數據(只有整數)。如果您打開了查詢字符串,則可能會將字符串'& per_page ='傳遞到您的模型上。如果用正則表達式或str_replace清理$ this-> limit。

0

這是你的錯誤

$products = $this->Products_model->get_products($limit, $uri_segment); 

$limit=the number of data you want to show in per page, 

$offset=the data number start from. 

所以點擊每個鏈接,你必須改變偏移值來顯示數據。試試這個

if($this->uri->segment(uri_segment) > 0){ 

       $offset=$this->uri->segment($uri_segment); 

      } 
      else{ 
       $offset=0; 

      } 
$products = $this->Products_model->get_products($limit, $offset);