title: Paginate text file with PHP
body: <p>I have a small script that displays blog posts from a text file, how can I add pagination so that it only shows 5 blog posts at a time?</p>
  
  <p>Here is the script:</p>
  
  <pre><code>&lt;html&gt; 
  &lt;head&gt;
  
  &lt;title&gt;blog&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt; 
  
  &lt;?php 
  
  $mode = 0; 
  if ($mode == 0) { $opFile = "blogfile.txt"; } 
  
  $fp = fopen($opFile,"r") or die("Error Reading File"); 
    $data = fread($fp, filesize($opFile)); 
  fclose($fp); 
  
  $line = explode("\n", $data); 
  $i=count($line); 
  
  for ($n=0 ; $n &lt; $i-1 ; $n++ ) { 
    $blog = explode("|", $line[$n]); 
  
    if (isset($blog[0])) 
     {     
      echo "&lt;div class=\"blog-post\"&gt;";
      echo "&lt;p class=\"blog-title\"&gt;".$blog[1]."&lt;/p&gt;";         
      echo "&lt;p class=\"blog-message\"&gt;".$blog[2]."&lt;/p&gt;"; 
      echo "&lt;p class=\"blog-date\"&gt;Posted: " .$blog[0]."&lt;/p&gt;";
      echo "&lt;div style=\"clear: both;\"&gt;&lt;/div&gt;";
      echo "&lt;/div&gt;";
  
   } 
   }        
  
  ?&gt; 
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is the text file:</p>
  
  <pre><code>Feb 17 2010|Title|Blog post content here|[end]
  Feb 17 2010|Title|Blog post content here|[end]
  Feb 17 2010|Title|Blog post content here|[end]
  Feb 17 2010|Title|Blog post content here|[end]
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>