[thelist] Splitting Articles across multiple pages with PHP

Ben Phillips ben at inchima.com
Thu Jan 31 03:50:01 CST 2002


> I have large documents that I would like to split across multiple pages
> using PHP and am tired of doing it manually. I want 200 words per
> page, and
> I need the links to be dynamically generated so the user can
> navigate to the
> resulting pages. The information is stored in a variable, not in
> a database.

A potential problem here would be passing the text between pages. Depends on
how you do this (you could obtain it from a database/file on every page)
then you should have no problems.

=========== CODE

// showpage.php
// pass it $p (page number)
// and $text

$pagelen = 200;
$numchars = strlen($text);

// get start position for page p
$startchar = ($p-1) * $pagelen;

// get total pages
$totpages = ceil($numchars / $pagelen);

// now output page
echo "=============<br>";
echo "page $p<br><br>";
echo substr($text, $startchar, 200);
echo "<br>=============<br>";

// output links
for ($i = 1; $i <= $totpages; $i++) {
  // say if this is current page
  if ($i == $p) {
    ?>you are on page $i<br><?
  } else {
    ?><a href="showpage.php?p=<?=$i?>">click here for page <?=$i?></a><br><?
  }
}

===================
Hope this helps. It hasn't been tested so there may (will) be errors.

Benji.
inchima.com




More information about the thelist mailing list