CodeIgniter Forums
Pagination - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Pagination (/showthread.php?tid=4342)



Pagination - El Forum - 11-19-2007

[eluser]skeletonfriend[/eluser]
How do I use the pagination class?

I have my controller, model, and view. In the view I have the correct amount of rows shown and pagination-links are there too, but when I try to click on of the page-links, I get an 404!

What can be the problem?

Code:
// controller

$config['base_url'] = base_url() . 'blog/';
$config['total_rows'] = $this->blog_model->getTotals();
$config['per_page'] = '20';
$this->pagination->initialize($config);
        
$num = $config['per_page'];
$offset = $this->uri->segment(3);
        
$data['blog_query'] = $this->blog_model->getBlogs($num, $offset);

// model

function getBlogs($num, $offset)
{
    $this->db->select('a.id, a.name, DATE_FORMAT(a.date_created, "%Y-%m-%d") AS date_created, a.author, CASE WHEN COUNT(b.comment_id) < 1 THEN "" ELSE CONCAT(COUNT(b.comment_id), " comments") END AS comment_total');
    $this->db->from('it_news a');
    $this->db->join('it_news_comments b', 'a.id = b.news_id', 'left');
    $this->db->groupby('a.id, a.name, a.author');
    $this->db->orderby('a.date_created DESC');
    $this->db->limit($num, $offset);
    $query = $this->db->get();
        
    if ($query->num_rows() > 0) { return $query->result(); }
}

// view

&lt;?php foreach ($blog_query as $row): ?&gt;
<div class="blogg-item">
    <h2>&lt;?=anchor('blog/post/'. $row->id, $row->name);?&gt;</h2>
    <p>Publicerad &lt;?=$row->date_created;?&gt; av <span>&lt;?=$row->author;?&gt; &lt;?=$row->comment_total;?&gt;</span></p>
</div>
&lt;?php endforeach;?&gt;
<div id="pager">&lt;?=$this->pagination->create_links();?&gt;</div>



Pagination - El Forum - 11-19-2007

[eluser]skeletonfriend[/eluser]
solved!


Pagination - El Forum - 11-19-2007

[eluser]Chris Newton[/eluser]
So, what was the solution?


Pagination - El Forum - 11-19-2007

[eluser]cinewbie81[/eluser]
base_url problem i believe


Pagination - El Forum - 11-20-2007

[eluser]skeletonfriend[/eluser]
Yepp, the problem was related to the base_url();

I changed:

Code:
$config['base_url'] = base_url() . 'blog/';

to:

Code:
$config['base_url'] = base_url() . 'blog/index';



Pagination - El Forum - 11-20-2007

[eluser]jvittetoe[/eluser]
Code:
'blog/index'
as in the index function of the blog controller? why would you need to specify the index function, isn't that called anyways.


Pagination - El Forum - 11-20-2007

[eluser]skeletonfriend[/eluser]
beats me.


Pagination - El Forum - 12-18-2007

[eluser]BravoAlpha[/eluser]
[quote author="skeletonfriend" date="1195522636"]
Code:
// controller
$config['base_url'] = base_url() . 'blog/';
[/quote]
The Pagination class will look for the page number at "uri_segment" 3 by default; However, in your `base_url`, it would be the second segment.


Pagination - El Forum - 12-19-2007

[eluser]JamesD[/eluser]
You could always change the 'uri_segment' to override the default segment too.

For example...
Code:
$config['uri_segment'] = 2;

-JamesD


Pagination - El Forum - 12-19-2007

[eluser]ejangi[/eluser]
You should really change that config item from:
Code:
$config['base_url'] = base_url() . 'blog/index';

to

Code:
$config['base_url'] = 'blog/index';

because all base_url() does is return the value for $config['base_url'] - so, you've got this recursive loop thing going on at the moment. It's probably not hurting your application, because as it stands base_url() is just returning a null string, but there's no point having it in your config item.

/system/helpers/url_helper.php:
Code:
/**
* Base URL
*
* Returns the "base_url" item from your config file
*
* @package        CodeIgniter
* @author        Rick Ellis
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @access    public
* @return    string
*/    
function base_url()
{
    $CI =& get_instance();
    return $CI->config->slash_item('base_url');
}