所以,我有一个问题在我的网站设置分页。
首先,如果我在我的网站的第一页,然后按去第2页中,“1”仍然停留大胆。 尽管我可以在我在第2页的网址看到。
其次,我需要一些帮助来正确地检索数据。 如果我在第1页,我想所有记录1-10。 如果我是第2页,我想从11-20获得的所有记录。 得到它了?
我已经google'd,并试图找到任何解决方案,没有任何成功。
$this->db->select('*');
$this->db->from('comments');
$this->db->where('comments.url_friendly', $id);
$this->db->join('allt', 'allt.url_friendly = comments.url_friendly', 'left');
$this->db->order_by('comments.date', 'asc');
$data['query'] = $this->db->get();
$data['title'] = 'Kommentarer - '.$data['query']->row()->amne;
$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $data['query']->num_rows();
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
另外,我正在使用的代码在岗2,在这个线程 。
是的,我是新来的笨,你可以看到。
$qry = "select * from comment c left join allt a on c.url_friendly = a.url_friendly where c.url_friendly = {$id}";
$per_page = 10;
$offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3):0);//this will get the current page(assuming that page number is in 3rd uri segment). if youy on page one,this will be set to zero
$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $this->db->query($qry)->num_rows();
$config['per_page'] = $per_page;
$config['uri_segment'] = 3; //this is dependent to where your page number displays in url
$this->pagination->initialize($config);
$qry .= " limit {$per_page} offset {$offset} ";
$data['result'] = $this->db->query($qry)->result();
$data['pagination'] = $this->pagination->create_links();
希望这将有助于。
试试这个代码后没有做适当的变化.........有由笨提供一个内置分页类。 您可以在用户手册中找到它。
在其中u要使用分页为零的函数定义开始索引变量。
public function pagination($start_index = 0)
{
$result = $this->model->get_results($data); //this $data is the argument which you are passing to your model function. If you are using database to get results array.
$items_per_page = 10; //this is constant variable which you need to define
$filtered_result = array_splice($result, $start_index, ITEM_PER_PAGE_USERS);
$model['$filtered_result'] = $filtered_result;
$total_rows = count($result);
$model['page_links'] = create_page_links (base_url()."/controlelr_name/pagination",ITEM_PER_PAGE_USERS, $total_rows);
$this->load->view('controller_name/view_file_name', $model);
}
function create_page_links($base_url, $per_page, $total_rows) {
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
此创建页面链接功能是一个通用的功能.............从用户指南的详细解释检查分页类......
$query = $this->db->query("SELECT COUNT(*) as jml from comments");
foreach ($query->result() as $row) {
$row = $row->jml;
} // to count all result in your table
$config['base_url'] = base_url() . 'comments/index/'; // set the base url for pagination
$config['total_rows'] = $row; // total rows
$config['per_page'] = '10'; // the number of per page for pagination
$config['uri_segment'] = 3; // see from base_url. 3 for this case
$config['first_link'] = 'Awal';
$config['last_link'] = 'Akhir';
$config['next_link'] = 'Selanjutnya';
$config['prev_link'] = 'Sebelumnya';
$this->pagination->initialize($config);
$data['comments'] = $this->MArtikel->getAllCommentsPagination($config['per_page'], $this->uri->segment(3));
// end pagination