Welcome Guest, Not a member yet? Register   Sign In
pagination beginner question
#1

[eluser]cehennem[/eluser]
Hi. I am trying to make a simple pagination test. Here is my default controller

Code:
<?
class main_page extends Controller {

    function main_page() {
        parent::Controller();
        $this->load->model('kitap_db');
        $this->load->library('pagination');
    }
    
    function index() {
        $kactane    = 3; // records per page
        $page        = $this->uri->segment(2); // page number
        if (empty($page)) $page = 0;
        
        $this->db->limit($kactane, $page);
        $sonuc = $this->db->get('kitaplar');
        
        if ($sonuc->num_rows() > 0) {
            foreach ($sonuc->result() as $birsonuc) {
                $veri['kitaplar'][] = $birsonuc;
            }
        }
        
        $config['uri_segment'] = 2;
        $config['num_links'] = 5;
        $config['per_page'] = $kactane;
        $config['base_url'] = 'http://localhost/test/page';
        $config['total_rows'] = $this->db->count_all_results('kitaplar');
        $this->pagination->initialize($config);
        $veri['sayfalar'] = $this->pagination->create_links();
        
        
        $this->load->view('anasayfa', $veri);
    }


}

And routes

Code:
$route['page/(:num)'] = "main_page/index/page/$1";

index.php is located under /test/

Now everything works ok on all pages page/1 page/2 etc. But when I go http://localhost/test/page/ (no page specified) I get 404 Not Found. I think here 'page' becomes a function name which does not exists. I want 1st page to be shown if no page specified like ..com/test/page/

I've tried to route it but couldn't success.

Thanks guys.
#2

[eluser]Narkboy[/eluser]
Man - wrote an entire reply then realised that I'd misread. Your code is a little confusing for me.

Basically - I avoid routing where possible. Let CI do the work.

At the moment, your $route is saying:

"a url with 'page/' then a number should be sent to the 'main_page' controller, should call the 'index' function, and that function should be given two variables - 'page' and the number in the url."

Ok - but your function dosen't want 2 variables. It dosen't want any!

If the index function only displays pagination, then you want:

Code:
function index($page_number) {
    if ( isset($page_number) ) {
        $page = $page_number;
    } else {
        $page = 0;
    }

... continue ...
}

And your route should be:
Code:
$route['page/(:num)'] = "main_page/index/$1";

Also, remember check that $page_number is exactly what you expect so that you don't get nasty security issues.

Think that'll do it!

/B




Theme © iAndrew 2016 - Forum software by © MyBB