Welcome Guest, Not a member yet? Register   Sign In
Pagination links screwed up
#1

[eluser]mikedfunk[/eluser]
I have 4 news items in my database and I limited the per_page to 1. When I run pagination I get this:

1 2 3 > last >

I only get 3 links. Even crazier: link 2 links to page 1, link 3 links to page 2, and last links to page 1. When I manually adjust the URL in the location bar it works fine, that's the kicker.

Here's my routing (I have htaccess set up to remove index.php):

Code:
$route['(.*)/news/(.*)'] = "news/index/$1/$2";

I don't think that should affect the pagination though.

Here's my controller:

Code:
// loaded pagination library of course or i wouldn't be getting the links

if ($this->uri->segment(4) == 'item') {
    
    // (code omitted) load the detail page for the item
    
} else { // if the segment is 'page' so I can separate the item view from the page view

    // load the list page with pagination
    
    $config['base_url'] = base_url().$reg.'/news/page/';
    $config['total_rows'] = '50';
    $config['per_page'] = '1';
    $config['uri_segment'] = 5;

    $this->pagination->initialize($config);
    
    $data['news'] = $this->db->get('news_articles', $config['per_page'], $this->uri->segment(5));

// and yes, i echoed the uri segment in the view to ensure it was returning the right number.
        
    $this->load->view('news_view', $data);

}

So I would type http://localhost/us/news/page/1 to get to a page.

And in the view the only thing of consequence is the create_links()

Code:
<?php foreach ($news->result() as $item): ?>
    // (code omitted) iterate through each news item and echo stuff from them...
    // of course there's only one right now because of the pagination limit.
<?php endforeach; ?>
<?php echo $this->pagination->create_links(); ?>

Why are my paging links screwed up? I read the documentation and read up on other threads, the usual answer seems to be to set the $config['uri_segment'] properly. I did that. What's the deal? :grrr:
#2

[eluser]hbr[/eluser]
hi mikedfunk,

I have exactly the same problem as you, I am fighting with it since yesterday but in vain.
Either there is a bug in the class, or the doc is not very clear about how to set the config up.
#3

[eluser]Sarfaraz Momin[/eluser]
Atlast someone has been facing some issues which I faced quite sometime back. What I did was write a small helper function to take of this issue. I don't know weather or not it would help you. But you can take a look at it.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
* Pagination helper.
* The one in CI simply doesn't work for me
*/
function pagination($url, $total, $perPage, $currentRecord)
{
    $maxPage = ceil($total / $perPage) - 1;
    $pageNum = $currentRecord;
    if(!$pageNum){
        $pageNum = 1;
    }
    $nav='';
    for($page = 1; $page <= $maxPage; $page++)
    {
        if ($page == $pageNum)
        {
            $nav .= "<span class=\"navbar-selected\">$page</span> "; // no need to create a link to current page
        }
        else
        {
            $nav .= "<span><a >$page</a></span>";
        }
    }

    if ($pageNum > 1)
    {
        $page  = $pageNum - 1;
        $prev  = " <a >[Prev]</a> ";
    }
    else
    {
        $prev  = '&nbsp;'; // we're on page one, don't print previous link
    }

    if ($pageNum < $maxPage)
    {
        $page = $pageNum + 1;
        $next = " <a >[Next]</a> ";

    }
    else
    {
        $next = '&nbsp;'; // we're on the last page, don't print next link
        $last = '&nbsp;'; // nor the last page link
    }
    $link = $prev . $nav . $next;
    return $link;
}
?&gt;
#4

[eluser]hbr[/eluser]
thanks for sharing it Wink

Here is the one I am using, but it is still a draft, I am going to clean it, change the variables and function names to be more significative.
But one thing is good about it, it works :p

The Library :

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pager {
    var $CI;
    var $current;
    var $total;
    var $page_size;
    var $range;
    var $size ;
    
    
    var $next;
    var $last;
    var $previous;
    var $first ;
    
    var $start    =1;
    var $stop     =1;
    
    var $css_link="";
    var $css_current="";
    var $css_pages="";
    
    var $add_link="";
    var $page_name="";
    var $base_url="";
    
    function Pager($p_total=1, $p_page_size=20, $p_range=5, $p_current=1) {
        $this->CI =& get_instance();
        
        $this->total = $p_total;
        $this->page_size = $p_page_size;
        $this->range = $p_range;        
        $this->current = $p_current;        
    }
    
    function set_css ($p_css = array()) {
        if (is_array($p_css)) {
            $this->css_link = "class=\"" . $p_css[0] . "\"";
            $this->css_current = "class=\"" . $p_css[1] . "\"";
            $this->css_pages = "class=\"" . $p_css[2] . "\"";
        }
    }
    
    function query_string($p_qs) {
        if ($p_qs) {
            $this->add_link = "&amp;$p_qs";
        }
        else {
            $this->add_link="";
        }
    }
    
    function create_links($p_current=1, $show_html=true) {
        $this->total = floor(($this->size / $this->page_size));
        if (($this->size % $this->page_size) != 0) $this->total += 1;
        if ($this->total <= 0) $this->total = 1;
        
        
        // current page
        $page = ($p_current) ? $p_current : $this->current;
        if ($page<=0) $page=1;
        
        // [inf, sup] range
        $r = $this->range;
        $t = $this->total;
                        
        // inf & sup interval
        $inf = floor($page/$r)*$r;
        $sup = $inf + $r;
        
        // inf never <= 0 and sup never > total number of pages
        if ($inf <= 0) {
            $inf = 1;
            $sup += $r;
        }
        else {
            $inf = $page - $r;
            $sup = $page + $r;
            
            if ($inf <=0) $inf = 1;
            if ($sup >$t) $sup = $t;
        }
        
        // previous page
        $pp = $page-1;
        if ($pp<=0) $pp=1;
        
        // next page
        $np = $page+1;
        if ($np>$t) $np=$t;
        
        
        $this->start = $inf ;
        $this->stop = $sup;
        
        
        if ($show_html) {        
            // html to display
            $html = "";
            $html .= "
            <a >css_link href='" . $this->base_url .  "1" . $this->add_link . "'><strong>|&lt;</strong></a>&nbsp;<a >css_link href='" . $this->base_url .  "$pp" .
                $this->add_link . "'><strong>&lt;&lt;</strong></a> &nbsp;
            ";
            
            // display pages
            for($n=$inf; $n<=$sup; $n++) {
                $current = $n;
                
                if ($current <= $this->total) {
                    if ($current == $page) {
                        $html .= "<strong>[$current]</strong>&nbsp;";
                    }
                    else {
                        $html .= "<a >css_link href='" . $this->base_url .  "$current" . $this->add_link . "'><strong>$current</strong></a>&nbsp;";
                    }
                }
            }
            
            
            
            $html .= "
             <a >css_link href='" . $this->base_url .   "$np" . $this->add_link . "'><strong>&gt;&gt;</strong></a>
            &nbsp;<a >css_link href='" . $this->base_url . "$t" . $this->add_link . "'><strong>&gt;|</strong></a>&nbsp;&nbsp;
            <strong>Page $page/$t</strong>
            ";
            
            return $html;
        }
    }
    
}
?&gt;


And how to use it :
Code:
$pg = new Pager();
$pg->total = 153;
$pg->range=4;
$pg->page_size=20;
$pg->show($current_page);

The result looks like this :

|< << 1 2 3 4 [5] 6 7 8 9 >> >|


Enjoy Wink




Theme © iAndrew 2016 - Forum software by © MyBB