Pagination for html files (split into multiple)


/ Published in: PHP
Save to your folder(s)

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 tag. Easy for end-users to set. Use CSS to style the pagination links (<ul> list)


Copy this code and paste it in your HTML
  1. <?
  2. // Specify the source file (e.g.: ../../files/myfile.html)
  3. $content = file_get_contents($srcfile);
  4.  
  5. // Add an extra <h1> tag to prevent it getting lost during explode
  6. $content = str_replace("<h1>", "<h1><h1 class=\"keep\">", $content);
  7.  
  8. // Explode the source file for each <h1> tag
  9. $explode = explode("<h1>", $content);
  10.  
  11. // Count the total number of <h1>'s. (My system needs -1, could be my mistake)
  12. $numpage = count($explode)-1;
  13.  
  14. // If not ?page= is set, then go by default to page 1 (first <h1>)
  15. $page = (empty($_GET['page']))?'1':$_GET['page'];
  16.  
  17. // Set (for later use) the current page +1 and -1
  18. $page_next = $page+1;
  19. $page_prev = $page-1;
  20.  
  21. // This shows the current page (could be more sophistaced ofcourse)
  22. echo($explode[$page]);
  23. ?>
  24.  
  25. <hr/>
  26.  
  27. <div id="pagination">
  28. <ul>
  29.  
  30. <? // If page is not 1, then show previous and first
  31. 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;?>
  32.  
  33. <? // Show each and every page available (1, 2, 3, 4...)
  34. for ($i=1; $i<=$numpage ; $i++) {
  35. 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>';} ?>
  36.  
  37. <? // Show next and last page when not already at last page
  38. 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;?>
  39.  
  40. </ul>
  41. </div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.