Welcome Guest, Not a member yet? Register   Sign In
Article or other text pagination helper
#1

[eluser]Jelmer[/eluser]
This is a very simple helper to paginate articles by a simple string put between the pages. It's meant to be called just before the text you want to output is put into the view and in this example your text is in the $content variable:
Code:
$this->load->helper('paginate_text');
$content = create_pages($content, $this->config->site_url(), $this->uri->uri_string());
The second and third parameters are needed to create the 1,2,3,4 links. It also takes 2 more parameters for extention and divider string:
Code:
$this->load->helper('paginate_text');
$content = create_pages($content, $this->config->site_url(), $this->uri->uri_string(), '.htm', '<p>{{NEWPAGE}}</p>');
Which puts .htm at the end of the links, the second part is the divider string with &lt;p&gt; tags as my content is generated by TinyMCE.

And here's the full helper paginate_text_helper.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function create_pages($full_story, $site_url, $page_nr_url, $ext = '', $divider = '<p>{{NEWPAGE}}</p>')
{
    // Return the string if no page divider is found
    if (!ereg($divider, $full_story)) return $full_story;
    
    // take the current page from the url or set to first page at the end of url
    ereg('page_[0-9]+', $page_nr_url, $page_nr);
    $page_nr = ereg_replace('page_', '', $page_nr[0]);
    if ($page_nr == '') { $page_nr_url .= '/page_1'; $page_nr = 1; }
    $page_url = $page_nr_url;
    
    // remove leading / to prevent double // after the domain
    $page_url = ereg_replace('^/', '', $page_url);
    
    // Convert to integer
    $page_nr += 0;
    
    // divide up the story
    $pages = explode($divider, $full_story);
    
    // create links with pagenumbers by ereg_replace to make sure the uri location stays the same
    for ($i = 1; $i <= count($pages); $i++) {
        if ($i != $page_nr)
            $page_links .= '<a class="pages_number" href="'.$site_url.ereg_replace('page_'.$page_nr, 'page_'.$i, $page_url).$ext.'">'.$i.'</a> | ';
        else
            $page_links .= '<strong>'.$i.'</strong> | ';
    }
    
    // create the first page and previous links (or just bold if that's the current location)
    if ($page_nr != 1)
        $firstprev = '<a class="pages_number" href="'.$site_url.ereg_replace('page_'.$page_nr, 'page_1', $page_url).$ext.'">&laquo; First</a> | <a class="pages_number" href="'.$site_url.ereg_replace('page_'.$page_nr, 'page_'.($page_nr - 1), $page_url).$ext.'">&lt;</a> | ';
    else
        $firstprev = '<strong>&laquo; First</strong> | <strong>&lt;</strong> | ';
    
    // create the last and next page links (or just bold if that's the current location)
    if ($page_nr != count($pages))
        $lastnext = '<a class="pages_number" href="'.$site_url.ereg_replace('page_'.$page_nr, 'page_'.($page_nr + 1), $page_url).$ext.'">&gt;</a> | <a class="pages_number" href="'.$site_url.ereg_replace('page_'.$page_nr, 'page_'.count($pages), $page_url).$ext.'">Last &raquo;</a>';
    else
        $lastnext = '<strong>&gt;</strong> | <strong>Last &raquo;</strong>';
    
    // composite the links for navigation
    $page_links = $firstprev . $page_links . $lastnext;
    
    // composite the output
    $output  = '<div class="page_numbers_top">'.$page_links.'</div>';
    $output .= '<div class="page_display">'.$pages[$page_nr - 1].'</div>';
    $output .= '<div class="page_numbers_bottom">'.$page_links.'</div>';
    
    // and return the lot
    return $output;
}

/* End of file pagina_helper.php */
/* Location: ./system/application/helpers/pagina_helper.php */
?&gt;
Everything with its own class to allow for easy styling. Also it will only work when there's not more than 1 thing paginated per URL (though changing that wouldn't really be a problem: just make the "page_" identifier a parameter).It doesn't care where in the url the page number is put, it will create links with the number at the same location as at which it was first found.

Question to others: is there a more efficient way to get the same functionality without using ereg & ereg_replace so much?

UPDATE: Now with previous and next links.
UPDATE 2: changed explanation mistake and added suggestion if you'd like to paginate more than 1 item per page.
#2

[eluser]theprodigy[/eluser]
I've actually written my own paginate class.

It functions very similarly to CI's paginate class with one main difference.

I didn't like the fact with CI's pagination class that if you had 10 pages and set the number of links to 2 (2 before and 2 after), but were on page 1, it only showed 3 links, and if you were on page 2, it would show 4 links, and from then on out to page 8, it would show the 5 links you requested. Then it would go back to 4 links on page 9, and 3 links on page 10.

My class asks for the number of pages you want shown, not the number of links before/after. My class also allows you to set things via a generic config function, or individual setter methods.

What is returned is an array:
Code:
array(
     first => entire anchor tag for the first link
     last  => entire anchor tag for the last link
     prev  => entire anchor tag for the previous link
     next  => entire anchor tag for the next link
     pages => string containing multiple anchor tags for the individual pages, and the current page non-link.
     )

The code, I believe, is very well documented. I tried having a couple people alpha test it, but I have yet to hear any response.

You can download it here: Paginate Class

Please let me know what you think of this class, whether good or bad. I would love to hear your input.

P.S. This is basic pagination, not article pagination like Jelmer's listed above.
#3

[eluser]theprodigy[/eluser]
I forgot to mention, it's made to be a library, not a helper. And it doesn't use any CI specific functionality, so it can be used outside CI applications (if you do that kinda stuff ;-) )
#4

[eluser]Jelmer[/eluser]
Wow, I forgot about this helper (even though it's integrated into a CMS of mine). There's a lot of inefficiency in it: the use of ereg instead of preg for regular expressions and ereg where also strpos(), str_replace() or even substr() could be used. Also the use of count() within the definition of a for-loop (which caused the function call on each iteration).
I'll rewrite this shortly to make it a lot more efficient.

Also: arguements 2,3 and 4 aren't necessary - they can all be gotten from the CI superobject with get_instance().

@theprodigy: this doesn't have anything to do with my helper, it should have it's own topic.
#5

[eluser]theprodigy[/eluser]
It's own thread has been created. You can view it here




Theme © iAndrew 2016 - Forum software by © MyBB