codeigniter分页中的404错误

时间:2014-03-30 01:36:22

标签: php codeigniter codeigniter-2 codeigniter-url codeigniter-routing

我看到很多这个问题,但我无法解决这个问题。

我怀疑它没有读取正确的段,因为我有一个重新路由配置和htaccess来删除索引页。

代码正在抛出404页。

这是我的模特:

    public function get_city_reviews($city_id,$limit,$offset) {

    $list = array();

    $this->db->from('biz');

    $this->db->where('city_id',$city_id);

    $this->db->order_by('created_at','desc');

    $this->db->limit($limit,$offset);

    $query = $this->db->get();
    foreach($query->result() as $row) {

    $list[] = $row;

       }

    return $list;

    }

我的控制器:

         $slug = $this->uri->segment(1,'');
    if($slug)
    {
        $this->load->model('catsAndCities','cc');
        $this->cc->set_table_name('city');
        $city = $this->cc->get($slug,'slug');

        if(!$city || $city->parent_id != 0)
        {
            show_404();
        }
        else 
        {
            $this->tank_auth->set_user_city($city);
        }
    }
    else 
    {
        $city = $this->tank_auth->get_user_city();
    }
    $this->load->library('pagination');

    $config = array();

    $base_url ="local/index/";

    $config["base_url"] = site_url().$base_url;

    $config["total_rows"] = $this->bizs->record_count();

    $config["per_page"] = 5;

    $config["uri_segment"] = 4;

    $config['full_tag_open'] = '<p class="pageBar">';

    $config['full_tag_close'] = '</p>';

    $config['num_links'] =2;

    $config['anchor_class'] = 'class="page-num" ';

    $this->pagination->initialize($config);

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

    $data["results"] = $this->bizs->get_city_reviews($city->id,$config["per_page"],  
    $page);

    $data["pagination_links"] = $this->pagination->create_links();

    $data['reviews'] =  $data["results"];

    $this->load->view('local/index',$data);

我的观点:

    <div id="pagination-container">
<div class="pagination">
    <ul>

       <?=$pagination_links?>

    </ul>
    </div>

这是我的路线:

   $route['default_controller'] = "local/city";
   $route['[0-9a-zA-Z\-_]+'] = "local/city";
   $route['scaffolding_trigger'] = "";

这是我的htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>


ErrorDocument 404 /index.php
</IfModule> 

2 个答案:

答案 0 :(得分:1)

正如我所怀疑的,如果找不到城市,您告诉CI找到404页面。试着评论一下:或者找一种方法解决它。

     if(!$city || $city->parent_id != 0)
    {
        show_404();
    }
    else 
    {
        $this->tank_auth->set_user_city($city);
    }

然后改变

   $base_url ="local/index/";

   $base_url ="";

你的细分为1

  $config["uri_segment"] = 1;

应该这样做。

答案 1 :(得分:0)

尝试将base_url更改为“local / city”,并将uri_segment更改为3.