2009年11月17日 星期二

[codeigniter] 分頁

//CI的分頁類,傳遞的參數是偏移量,
//也就是從庫中的第幾條記錄開始顯示,
//而不是某些分頁方法中所傳遞的頁碼

function show_all($offset)
{
  //載入'分頁類'
  $this->load->library('pagination');
  $this->load->helper('form');
  $this->load->helper('url');
  //根據組合條件,計算記錄總數,(當前組合條件為空)
  $config['total_rows'] = $this->db->count_all_results('mytable');
  //設置本頁路徑
  $config['base_url'] = "本頁路徑";
  //設置每頁顯示記錄數
  $config['per_page'] = '15';
  //設置分頁導航條樣式
  $config['full_tag_open']   = '<div id = "page_nav">';
  $config['full_tag_close']  = '</div>';
  $config['first_link']      = '首頁';
  $config['last_link']       = '末頁';
  $config['next_link']       = '下一頁>';
  $config['prev_link']       = '<上一頁';
  //應用設置
  $this->pagination->initialize($config);
  //設置查詢條件
  $this->db->select('你的字段列表');
  //排序順序
  $this->db->order_by("id", "desc");
  //limit(結果數,偏移量)
  $this->db->limit($config['per_page'],$offset);
  //查詢
  $query = $this->db->get('mytable');
  //顯示結果列表
  foreach ($query->result_array() as $row){
    $this->table->add_row($row);
  }
  echo $this->table->generate();
  //添加分頁導航條
  echo $this->pagination->create_links();
}

【下列文章您可能也有興趣】

沒有留言: