【问题标题】:When I click to the next page on pagination,it goes to 404 error in codeigniter当我点击分页的下一页时,它会在 codeigniter 中出现 404 错误
【发布时间】:2015-06-22 09:47:38
【问题描述】:

在我的News.phpcontroller 中,我有以下代码:

 public function index()
{
    $data['title'] = 'Database Details';

    $config     =   array();
    $config['base_url'] =   base_url("news/index");
    $config['total_rows'] = $this->news_model->record_count();
    $config['per_page']     =   5;
    $config['uri_segment'] = 3;
    //$config['use_page_numbers'] = TRUE;
 // $config['page_query_string'] = TRUE;

    $choice  =  $config["total_rows"] / $config["per_page"];
    $config["num_links"] =  round($choice);

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

    $page   = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    //echo "page--".$page;
    $data['user_data']  = $this->news_model->get_details($config['per_page'],$page);
    $data['links']          =  $this->pagination->create_links();

    $this->load->view('templates/header');   
    echo $this->db->last_query();       

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

    $this->load->view('templates/footer');
}

在我的News_Model.php模型中,存在以下代码:

 public function get_details($limit,$start)
{    
    //echo "Limit---".$limit."</br>";
    //echo "Start---".$start."</br>";
      $this->db->limit($limit,$start);
      $query = $this->db->get('user_data'); 
      return $query->result_array();    

 }

我的视图文件index.php 显示:

 <h3>
   <?php echo $title; ?>
 </h3>
 <table>
    <thead>
      <th>Name</th>
      <th>Address</th>
      <th>Email</th>
       <th>Mobile</th>
    </thead>
   <tbody>
   <?php
       //print_r($user_data);
     foreach ($user_data as $user_item): 
   ?>
   <tr>

      <td><?php echo $user_item["user_name"];?></td>
     <td><?php echo $user_item["address"];?></td>
     <td><?php echo $user_item["email"];?></td>
     <td><?php echo $user_item["mobile"];?></td>
     <td><a href="edit?id=<?php echo $user_item["id"];?>">Edit</a></td>
     <td><a href="delete?id=<?php echo $user_item["id"];?>">Delete</a></td>
   </tr>
<?php endforeach ?>

 </tbody>
 </table>
 <p class="pagination"><?php echo $links; ?></p>

在我的config.php

 <?php
      $config['base_url'] = 'http://localhost/CodeIgniter-3.0.0/';
      $config['index_page'] = 'index.php';
 ?>

如果我删除 $config['index_page'] = '';我的其他页面没有显示。

在我的route.php

  <?php
    $route['news/(:any)'] = 'news/index/$1'; //explain what does it actually means?
    $route['news'] = 'news';
    $route['default_controller'] = 'news/create';
    $route['(:any)'] ='pages/view/$1';

 ?>

以上代码显示如下:

但是当我单击page 2 时,它显示为:

请不要推荐Link1Link2Link3。我看过上面的链接但不知道如何获得它:(

请指导我!!

【问题讨论】:

  • 打开config.php更改$config['index_page']='index.php';
  • @viral 它已经像你提到的那样了
  • @Viral 当我的链接是这样的:localhost/CodeIgniter-3.0.0/index.php/news/index/2 这将显示。现在我想知道如何在这两者之间获取 index.php?
  • 如果您输入index.php 是否有效??
  • @Abdulla 同时我的查询是 SELECT * FROM user_data` LIMIT 2, 5` 它的错误知道:( 它应该在第 5 条记录之后知道,例如 SELECT * FROM user_data` LIMIT 5, 5`儿子上

标签: php codeigniter pagination


【解决方案1】:

在 config.php 中

$config['base_url'] = 'https://stackoverflow.com/';
$config['index_page'] = '';

在你的路由器中

$route['news/(:any)'] = 'news/$1';
$route['news'] = 'news';
$route['default_controller'] = 'news/create';
$route['(:any)'] ='pages/view/$1';

并放置 .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

编辑 01

<?php

    $data['title'] = 'Database Details';

    $count = $this->news_model->record_count()

    $config['base_url'] =   base_url(). 'index.php/news/index';
    $config['total_rows'] = $count;
    $config['per_page']     =   5;
    $config['uri_segment'] = 3;
    $limit = $config['per_page'];

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

    $page   = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['user_data']  = $this->news_model->get_details($limit,$page);
    $data['links']    =  $this->pagination->create_links();


    $this->load->view('templates/header');   
    $this->load->view('news/index', $data);   
    $this->load->view('templates/footer');

【讨论】:

  • 我试过了 ji....但同样的事情正在发生:(但是你能解释一下 htaccess 的用途是什么吗?
  • 它的删除`/index.php` 部分在您的网址中
  • 是的,我的网址是这样的http://localhost/CodeIgniter/news/index/5。但这就是我之前得到的:(
  • $route['news/(:any)'] = 'news/index/$1'; 留空并检查一次
  • 你的意思是$route['news/(:any)'] = '';?
【解决方案2】:

删除$config['use_page_numbers'] = TRUE;,然后检查。

还有为什么你有 2 条路线:

$route['news/(:any)'] = 'news/index/$1'; //explain what does it actually means?
$route['news'] = 'news';

【讨论】:

  • 当我点击第二页时,它会在我的地址中显示http://localhost/CodeIgniter-3.0.0/news/index/5:(
  • 当我的链接是这样的:http://localhost/CodeIgniter-3.0.0/index.php/news/index/2 这将显示。现在我想知道如何在这两者之间获得index.php
  • 我接受它删除use_page_numbers,因为当我删除它时,我可以解决查询问题,但页面转到404
猜你喜欢
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多