Return to Snippet

Revision: 14164
at May 21, 2009 09:33 by axtg

Initial Code
// Specify the source file (e.g.: ../../files/myfile.html)
$content = file_get_contents($srcfile);

// Add an extra <h1> tag to prevent it getting lost during explode
$content = str_replace("<h1>", "<h1><h1 class=\"keep\">", $content);

// Explode the source file for each <h1> tag
$explode = explode("<h1>", $content);

// Count the total number of <h1>'s. (My system needs -1, could be my mistake)
$numpage = count($explode)-1;

// If not ?page= is set, then go by default to page 1 (first <h1>)
$page = (empty($_GET['page']))?'1':$_GET['page'];

// Set (for later use) the current page +1 and -1
$page_next = $page+1;
$page_prev = $page-1;

// This shows the current page (could be more sophistaced ofcourse)


<div id="pagination">

<? // If page is not 1, then show previous and first
echo ($page!='1')?'<li><a href="?module=cms&amp;cms_id='.$_GET['cms_id'].'&amp;page=1">Eerste</a></li><li><a href="?module=cms&amp;cms_id='.$_GET['cms_id'].'&amp;page='.$page_prev.'">Vorige</a></li>':null;?>

<? // Show each and every page available (1, 2, 3, 4...)
for ($i=1; $i<=$numpage ; $i++) { 
echo ($i==$page)?'<li><strong>'.$i.'</strong></li>':'<li><a href="?module=cms&amp;cms_id='.$_GET['cms_id'].'&amp;page='.$i.'">'.$i.'</a></li>';} ?>

<? // Show next and last page when not already at last page
echo ($page!=$numpage)?'<li><a href="?module=cms&amp;cms_id='.$_GET['cms_id'].'&amp;page='.$page_next.'">Volgende</a></li><li><a href="?module=cms&amp;cms_id='.$_GET['cms_id'].'&amp;page='.$numpage.'">Laatste</a></li>':null;?>


Initial URL

Initial Description
For a client I needed a bit of code that could split one file (html, editable through wysiwyg editor) to multiple files, since all files would by default become very long. This code splits one file by the <h1> tag. Easy for end-users to set. Use CSS to style the pagination links (<ul> list)

Initial Title
Pagination for html files (split into multiple)

Initial Tags
database, files

Initial Language