2015-06-22 82 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 –