2015-06-22 86 views
1

在我News.php控制器,我有下面的代码:当我点击分页上的下一个页面,它会以404错误笨

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'; 
?> 

如果删除$配置[ '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'; 

?> 

上面的代码显示类似如下:

enter image description here

但是当我点击page 2它显示为: enter image description here

请不要建议Link1,Link2Link3。我已经看过上面的链接,但不知道如何得到它:(

请亲引导我!

+0

开'config.php'改变'$配置[ 'index_page'] = '的index.php';' – Viral

+0

@viral其已经喜欢你提到的只有 –

+0

@Viral当我的链接是这样的:http://localhost/CodeIgniter-3.0.0/index.php/news/index/2它将显示。现在我想知道如何获得index.php之间呢? –

回答

2

在config.php

$config['base_url'] = ''; 
$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'); 
+0

我试过了它ji ....但同样的事情发生:(但你能解释什么是htaccess的使用? –

+0

其删除'/ index.php'部分在您的url –

+0

是ji,我的网址是这样的'http:// localhost/CodeIgniter/news/index/5'。但这是我从前面得到的: –

0

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

也是为什么你有2种途径:

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

当我点击第二页时,它会显示'http://localhost/CodeIgniter-3.0。0 /新闻/索引/ 5'在我的地址:( –

+0

当我的链接是:'http:// localhost/CodeIgniter-3.0.0/index.php/news/index/2'这将显示。现在我想知道如何获得'index.php'之间的那个? –

+0

我接受它删除'use_page_numbers',因为当我删除它,我可以得到解决查询问题,但页面转到404 –