Dan
Dan

Reputation: 2174

how to show pagination information using codeigniter

i want to show pagination information in my page. like this Below table is showing 0-8 books out of 17 books

As explained below in comments and answer i tried to fix my problem. But still i have problem.

  1. Unable to get the cur_page value. I tried $CI =& get_instance(); $curpage=$CI->pagination->cur_page; But this is giving me a
    zero value.

  2. if i supply manual value for cur_page . say $cur_page=3, and my total record is 17. Then at the last page(i.e 3) it is showing wrong information showing 24-17 of 17 books

Please help me to fix this problem?

In my view page

 <?php    echo "displaying $result_start to $result_end of $total";?>

In controller

 $config = array();  
    $config['base_url'] = base_url().'viewallbooks/books/pgn/';
    $config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
    $config['uri_segment'] = 4;
    $config['per_page'] = 8;
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>'; 

       $this->pagination->initialize($config);
       $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

    $data["query"] = $this->Subjectmodel->get_subjects();
    $data["query1"]=  $this->Editionmodel->get_edition();

    /*
     * For showing the "showing 8-16 of 23 data
     */
    $data["flimit"]=$config["per_page"];
    $data["slimit"]=$page;
    $data["trows"]= $config["total_rows"] ; 
     /*
     * Ends here--For showing the "showing 8-16 of 23 data
     */

    $data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
     $data["links"] = $this->pagination->create_links();
    $this->load->view('commonfiles/booksview',$data);   

In Model for counting total number of rows

  //-- -Counting the rows of bookdetails of table where display_id is as  argument----- 
 public function record_count_for_secondtopBooks($id) {
    $this->load->database(); 
     $this->db->where('display_id', $id);
     $this->db->from('bookdetails');
    return $this->db->count_all_results();
}

Upvotes: 4

Views: 4856

Answers (2)

Shahzad Nasir
Shahzad Nasir

Reputation: 168

I used this Code for generating pagination information

if($config["total_rows"]>10){
    if($this->pagination->cur_page==1)
        $start = $this->pagination->cur_page;
    else
        $start = (($this->pagination->cur_page-1) * $this->pagination->per_page)+1;

    if($this->pagination->cur_page * $this->pagination->per_page > $config["total_rows"])
        $end = $config["total_rows"];
    else
        $end = $this->pagination->cur_page * $this->pagination->per_page;

    $data['pagination_des'] = "Showing ".($start)." to ".($end)." from ". $config["total_rows"]." results";
}else
{
    $data['pagination_des'] = "Showing ".($this->pagination->cur_page+1)." to ".($config["total_rows"])." from ". $config["total_rows"]." results";
}

Upvotes: 3

Filippo oretti
Filippo oretti

Reputation: 49817

var_dump($this->pagination); returns somenthing like this:

object(CI_Pagination)
21 (31) { 
    ["base_url"]=> string(31) "http://localhost/site/users" 
    ["prefix"]=> string(0) "" 
    ["suffix"]=> string(0) "" 
    ["total_rows"]=> int(3) 
    ["per_page"]=> int(20) 
    ["num_links"]=> int(15) 
    ["cur_page"]=> int(0) 
    ["first_link"]=> string(5) "First" 
    ["next_link"]=> bool(false) 
    ["prev_link"]=> bool(false) 
    ["last_link"]=> string(4) "Last" 
    ["uri_segment"]=> int(2) 
    ["full_tag_open"]=> string(25) "" 
    ["full_tag_close"]=> string(4) "" 
    ["first_tag_open"]=> string(31) "" 
    ["first_tag_close"]=> string(13) " " 
    ["last_tag_open"]=> string(36) " " 
    ["last_tag_close"]=> string(7) "" 
    ["first_url"]=> string(31) "http://localhost/site/users" 
    ["cur_tag_open"]=> string(87) " " 
    ["cur_tag_close"]=> string(11) "" 
    ["next_tag_open"]=> string(36) " " 
    ["next_tag_close"]=> string(13) " " 
    ["prev_tag_open"]=> string(36) " " 
    ["prev_tag_close"]=> string(7) "" 
    ["num_tag_open"]=> string(35) " " 
    ["num_tag_close"]=> string(7) "" 
    ["page_query_string"]=> bool(false) 
    ["query_string_segment"]=> string(8) "per_page" 
    ["display_pages"]=> bool(true) 
    ["anchor_class"]=> string(26) "class="button bradiusMax" " }

i should do:

echo "Showing ".( $this->pagination->cur_page * $this->pagination->per_page)." of ". $this->pagination->total_rows." total results";

if you want you can use also different formats as itachi showed you in comments.

Upvotes: 1

Related Questions