CodeIgniter Forums
Please, I am getting crazy with the pagination class :( - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Please, I am getting crazy with the pagination class :( (/showthread.php?tid=32255)



Please, I am getting crazy with the pagination class :( - El Forum - 07-17-2010

[eluser]victorche[/eluser]
Here is my model:
Code:
function get_posts($limit, $offset)
    {
        $query = $this->db
            ->select('*')
            ->from('articles')
            ->order_by('id', 'desc')
            ->limit($limit, $offset)
            ->get();

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $entries[] = array(
                    'title' => $row['title'],
                    'body' => character_limiter($row['body'], 800)
                );
            }
            return $entries;
        }
    }

    function count_posts()
    {
        return $this->db->count_all_results('articles');
    }

And this is my controller:

Code:
function index($page = NULL)
    {
        $this->load->model('articles_model');

        $limit = 2;

        if (isset($page) && is_numeric($page))
        {
            $offset = ($page - 1) * $limit;
        }
        else
        {
            $offset = 0;
        }

        $count = $this->articles_model->count_posts();
        $entries = $this->articles_model->get_posts($limit, $offset);

        $config['base_url'] = site_url('articles');
        $config['per_page'] = $limit;
        $config['total_rows'] = $count;

        $this->pagination->initialize($config);
        $pages = $this->pagination->create_links();

        $data = array(
            'articles_heading' => 'Articles Heading',
            'articles_entries' => $entries,
            'articles_pages' => $pages
        );
        }

The links are generated normally, but anyway when I click on page 2, I always have 404 error Sad

Please, help! I've read 200 topics about it and still not success Sad


Please, I am getting crazy with the pagination class :( - El Forum - 07-17-2010

[eluser]Althalos[/eluser]
I'm not a pro myself, but my guess is that it would work if you would put $config['base_url'] = site_url('articles/index');

I can see two things that could go wrong when you put $config['base_url'] = site_url('articles');

1) The pagination class looks up the third URI segment by default. In your case, the page number would actually be second segment
2) The second segment normally is the method. So if you link to example.com/articles/2 instead of interpreting that as page two, it might look for a method called 2 and this might be what causes the 404.


Please, I am getting crazy with the pagination class :( - El Forum - 07-17-2010

[eluser]victorche[/eluser]
Thanks, really! This helped a little... You're right, now it works. Anyway it adds /index/ to the pagination links, something which I completely don't want to happen Sad

How should I do it, without adding $config['base_url'] = site_url('articles/index');

I want my pages to be
site.com/articles/2
or
site.com/articles/page:2

but without /index/


Please, I am getting crazy with the pagination class :( - El Forum - 07-18-2010

[eluser]Althalos[/eluser]
I believe you want to do this: http://ellislab.com/forums/viewthread/88534/


Please, I am getting crazy with the pagination class :( - El Forum - 07-18-2010

[eluser]Rolly1971[/eluser]
try adding a new route in application/config/route.php

for the first style:

route['articles/:num'] = 'articles';

if you use the second type:

route['articles/:any'] = 'articles';

this will default a url like: http://site.com/articles/2 to the articles controller -> index function.

so long as you have your index function reading and handling the uri segments properly this will work a charm


Please, I am getting crazy with the pagination class :( - El Forum - 07-26-2010

[eluser]victorche[/eluser]
Thanks a lot ... Adding a route rule fixed the problem :]

Now another question (related with the topic) ... Can I have something like
/articles/page:2
instead of just
/articles/2
?


Please, I am getting crazy with the pagination class :( - El Forum - 07-26-2010

[eluser]Althalos[/eluser]
I would suggest opting for a URL like this: example.com/articles/page/2/user/username

And then in your controller, put this:
$uri = $this->uri->uri_to_assoc(2);
echo $uri['page']; //Echos "2"
echo $uri['user']; //Echo "username"


Please, I am getting crazy with the pagination class :( - El Forum - 07-26-2010

[eluser]victorche[/eluser]
[quote author="Althalos" date="1280170813"]I would suggest opting for a URL like this: example.com/articles/page/2/user/username

And then in your controller, put this:
$uri = $this->uri->uri_to_assoc(2);
echo $uri['page']; //Echos "2"
echo $uri['username']; //Echo "username"[/quote]

No, no ... articles part has nothing to do with users. Articles are submitted only by admins.
I just want instead of only page number
articles/2
to have a link like:
articles/page:2

This makes more sense, as ... /articles/2 can mean like you are reading an article with id=2

but by adding page:2 it really makes sense.


Please, I am getting crazy with the pagination class :( - El Forum - 07-26-2010

[eluser]Althalos[/eluser]
I didn't give you a solution so that you didn't have to think. I gave you an example, so that you could understand one way to do what you wanted to do. The username part had a purely explanatory purpose.

I understand why articles/page:2 is an improvement over articles/2, but articles/page/2 does exactly the same thing. It just looks a little bit different, but it is by far more common than separating values with :.

Maybe this is why the format I suggested is supported in the URI class whereas the format you suggest is not.

However, if you want to use your format you can still achieve this. It wouldn't be as beautiful a solution, but it'd work:
$pageId = str_replcae('page:', '', $this->uri->segment(2));