Welcome Guest, Not a member yet? Register   Sign In
[solved] Pagination 404 on other pages
#1
Rainbow 
(This post was last modified: 11-02-2015, 07:57 AM by artigianidelweb.)

Hello everybody!   Smile 


I have a question for you Big Grin 


I have created that controller class for my blog:




PHP Code:
<?php

Class Blog extends CI_Controller {
    
    

    public function 
__construct(){
        
parent::__construct(); 
            
        
$this->load->model('Blog_m');
        
$this->load->library("pagination");

    }
    

    
    public function 
index()
    {




                
$config = array();
                
$config["base_url"] = base_url() . 'index.php/blog';
                
$config["total_rows"] = $this->Blog_m->record_count();
                
$config["per_page"] = 2;
                
$config["uri_segment"] = 3;
                
$config['use_page_numbers'] = TRUE;
            
                
$this->pagination->initialize($config);
                
                
//$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
                
$page $this->uri->segment(3);
                
$data["rows"] = $this->Blog_m->getPosts($config["per_page"], $page);
                
$data["links"] = $this->pagination->create_links();
                        
            
                
$this->load->view('blog'$data);
                    


    }
    
    
    
    
    
    
    
        
    
    



My model:


PHP Code:
class Blog_m extends CI_Model {
        
    
    public function 
getPosts($limit$start) {
        
        
    
        
$this->db->limit($limit$start);
        
$this->db->select('blog_title,blog_description,blog_id'); 
        
$this->db->from('blog');
        
        
$q $this->db->get();        
        
        if (
$q->num_rows() > ) {
            
            foreach (
$q->result() as $row) {
                
$data[] = $row
            }
            return 
$data;
        }
        
        return 
false;
    }

[...] 

My view:


PHP Code:
            <?php foreach($rows as $r) : ?>    
            <h3><?php echo $r->blog_title?></h3>    
            <p><a href="blog/view/<?php echo $r->blog_id?>" > <?php echo $r->blog_description?>    </a>
            </p>        
        <?php endforeach; ?>
    
    
<p><?php echo $links?></p> 







So, the result view it'OK,
i have on  http://localhost/codeigniter1/index.php/blog
a page with 2 post and at the bottom the paginator with CORRECT LINKS,
es:

2 ----  links to --> http://localhost/codeigniter1/index.php/blog/2
3 ----  links to --> http://localhost/codeigniter1/index.php/blog/3


but if i click on it, on the other pages,
it returns 404 Page Not Found




What I missed ?

Thank you so much!
Big Grin
Reply
#2

(This post was last modified: 11-02-2015, 01:54 AM by Martin7483.)

CodeIgniter is now trying to locate a method called 2 and 3 as your urls are /blog/2 and /blog/3.
Add a detail method to your blog controller for displaying a single blog and change your urls to /blog/detail/2 /blog/detail/3
Reply
#3

Sorry,
maybe i have not exlained well.

The link to the SINGLE view works perfectly,
here i have omitted code for that cause it works,
http://localhost/codeigniter1/index.php/blog/view/2
http://localhost/codeigniter1/index.php/blog/view/1

---> are ok.

The probleme is in the PAGINATION LINKS instead,
errors are in the pagination links,

as

http://localhost/codeigniter1/index.php/blog/2
http://localhost/codeigniter1/index.php/blog/3

wich shoul show post dfrom x- to y
and from y - to z etc

Thank you so much!
Reply
#4

Oke, did not get that from your post Smile
You can do one of the following methods

1) Add this to your routes config file located at ./application/config/routes.php
PHP Code:
$route['blog/(:any)'] = 'blog/index'

2) You can add the _remap method to your controller
PHP Code:
public function _remap($method) {
 
   ifmethod_exists($this$method) ) {
 
       $this->{$method}();
 
   } else {
 
       // Method is your arg value for the index method
 
       $this->index($method);
 
   }


If you want to use the _remap method, change your index method to this
PHP Code:
public function index($page NULL) {
 
   ifis_null($page) ) {
 
       $page 0;
 
   }
 
   // The rest of your index method


Don't do both Wink
Reply
#5

(This post was last modified: 11-02-2015, 06:37 AM by artigianidelweb.)

Ok thanks.
I tied both but there is no way to work, each has a problem.

with

PHP Code:
$route['blog/(:any)'] = 'blog/index'

there is no more the 404 but it doesn'load any other pages than the first one.

With the second eaxample doens't works the single view too.

So, coudl you please post me an example (just any kind of example) on CI that works?

Just a controller and model to create
a news section
like /news
with pagination like /news/1
and single view like /news/view/1

?

So i can study it better.

Thank you so much!

Angel
Reply
#6

Ok,
I got it.

Simply,
the uri segment was 2, no 3 or 4!

Big Grin

Thank you so much guys! Cool
Reply




Theme © iAndrew 2016 - Forum software by © MyBB