Codeigniter, setting up pagination

拈花ヽ惹草 提交于 2019-12-31 06:17:30

问题


So, i'm having a problem to setting up pagination on my site.

First of all, if i'm at the first page of my site, and pressing to go to page 2, the "1" still stays bold. Though i can see in the url that i am at page 2.

Secondly, i need some help to retrieve the data correctly. If i am at page 1, i want to get all records 1-10. If i am on page 2, i want to get all records from 11-20. Got it?

I have google'd and tried to find any solution, without any success.

$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();

Also, i'm using the code in post 2, in this thread.

And yes, i'm new to Codeigniter, as you can see.


回答1:


$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();

hope this will help.




回答2:


Try this code after u done proper changes......... There is an inbuilt Pagination class provided by codeIgniter. You can find it in user guide.

Define a start index variable in the function where u want to use pagination as zero.

  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();
   }

This create page links function is a generic function.............for more explanation check pagination class from user guide......




回答3:


$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


来源:https://stackoverflow.com/questions/13572085/codeigniter-setting-up-pagination

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!