Codeigniter分页链接不起作用。显示404页面。

时间:2015-08-23 15:31:02

标签: php codeigniter pagination

这是我第一次做分页。一切正常,除了1 2 3 ....链接部分在我看来。数据库的结果也很完美。但每当我点击2时,它就会导致404页面。

我的观看链接:http://localhost/learn/site/index/ 当我点击2:http://localhost/learn/site/index/10

我的控制器:

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

}`

以下是我的观点:

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

          ?>

谁能告诉我问题出在哪里?我被困在这里。

1 个答案:

答案 0 :(得分:0)

index控制器中设置site方法,如下所示

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

错误发生在$config['base_url'] = base_url().'site/index'; ;这一行。查看文件没问题

相关问题