2
votes

This is my first time doing pagination. Everything working fine except the the 1 2 3.... link part on my view. The result from database also showing perfectly. But whenever I click on the 2 it leads to a 404 page.

My view link: http://localhost/learn/site/index/ When I click on the 2 : http://localhost/learn/site/index/10

My controller:

public function index(){
        $this->load->model('model_users');
        //--------------------Pagination----------------------
        $this->load->library('pagination');
        $config['base_url'] = base_url().'site/index'; ;
        $config['total_rows'] = $this->db->get('posts')->num_rows();
        $config['uri_segment'] = 3;
        $config['per_page'] = 10; 
        $this->pagination->initialize($config); 

        $pag = $this->db->order_by("post_id", "desc")->get('posts', $config['per_page'], $this->uri->segment(3));        
        $table_data_home['allposts'] = $pag->result();

        //------------end pagination-------------------------


        $this->load->view('my_site',$table_data_home);

}`

Here is my view:

         <?php
foreach($allposts as $posts){
    $title = $posts->post_title;
    $name = $posts->name;
    $categories = $posts->category;
    $post = $posts->post;
    $pic = $posts->pic;
    $time = $posts->time;
    $post_id = $posts->post_id;
        ?>
          <div id="allsinglepost"> <a href="<?php echo site_url('site/full_post'); ?>?post_id=<?php echo $post_id ?>">
            <h3><?php echo $title;?></h3>
            </a>
            <hr class="post_inside_hr">
            <h6><b>Written By :</b> <?php echo $name ;?></h6>
            <h6><b>Date:</b>
              <?php  echo substr($time,0,10).'; Time: '.substr($time,11,12); ?>
            </h6>
            <hr class="post_inside_hr">
            <div id="post_description">
              <p><?php echo substr($post,0,200) ;?> ........</p>
            </div>
          </div>
          <hr id="postbreakline">
          <?php } ?>
          <!--The upper part will be a FOREACH for posts--> 

          <!-- pagination part -->
          <?php
          echo $this->pagination->create_links();

          ?>

Can anyone tell me where is the problem? I am stuck here.

1
change this $config['base_url'] = base_url().'site/index'; ; as $config['base_url'] = site_url('site/index'); - Rejoanul Alam
Wow Its working now. Thank you. :D - ZIS
@RejoanulAlam: make your comment an anwser, otherwise this post doesn't make sense - Vickel
@ZahidulIslamSuzon If that solve your problem then You may accept my answer. Thanks - Rejoanul Alam

1 Answers

0
votes

Make your index method in site controller as follows

public function index(){
    $this->load->model('model_users');
    //--------------------Pagination----------------------
    $this->load->library('pagination');
    $config['base_url'] = site_url('site/index');
    $config['total_rows'] = $this->db->get('posts')->num_rows();
    $config['uri_segment'] = 3;
    $config['per_page'] = 10; 
    $this->pagination->initialize($config); 

    $pag = $this->db->order_by("post_id", "desc")->get('posts', $config['per_page'], $this->uri->segment(3));        
    $table_data_home['allposts'] = $pag->result();

    //------------end pagination-------------------------


    $this->load->view('my_site',$table_data_home);
}

the error was in $config['base_url'] = base_url().'site/index'; ; this line. View file is ok