Web Development

How To Put Your Articles Into Separate Pages

How To Put Your Articles Into Seperate Pages

This is a cool little way to put articles from a page or database into seperate pages. This can be done quite a few ways but here’s one of them. So lets get into it!

The Article Part

Lets say this is your (extremely short) article:

Hello,

This article is about nothing much… This is just here for a page example.

I could go on forever… bla bla bla bla bla bla bla bla bla bla

And again… bla bla bla bla bla bla bla bla bla bla bla.

OK, you want to “cut up” the article into 3 seperate pages so that it is displayed better and is easier to read. This is what you would add to your article. You’ll need to add a little bit of PHP:


<?php
$pages = "3"; // How many pages you want on the page
$article = <<<EOF
Hello,

This article is about nothing much... This is just here for a page example.

{ENDPAGE1}
I could go on forever... bla bla bla bla bla bla bla bla bla bla

{ENDPAGE2}
And again... bla bla bla bla bla bla bla bla bla bla bla.

{ENDPAGE3}
EOF;?>

The Code

Now that your article has been “cut up”, you’ll need this code belwo the above code:


<?php
// Remove this code below if you don't
// what it to add breaks (<BR<) automatically.
$article = nl2br($article);

// Make sure there are no stuff ups.
if ($pages < 1) {
if (!$page || $page <= "0") {
$page = "1";
}
$article = "{ENDPAGE0} ".$article;
$pagea = $page - 1;
$pageb = $page + 1;

// Check that the page exists
$match_count = preg_match_all("/{ENDPAGE"."$pagea"."}(.*?){ENDPAGE"."$page"."}/is", $article, $matches);
if ($match_count == "" || $match_count == "0") {
$article = "This page does not exist!";
} else {
if( preg_match( "/{ENDPAGE"."$pagea"."}(.*?){ENDPAGE"."$page"."}/is", $article, $match ) )
$article = $match[1];
}

// Generate Page Numbers
if ($page < "1") {
$prev = "<a href=\"$PHP_SELF?page=$pagea\"<<< Prev</a< :";
} else {
$prev = "<< Prev :";
}
if ($page < "$pages") {
$next = ": <a href=\"$PHP_SELF?page=$pageb\"<Next <<</a<";
} else {
$next = ": Next <<";
}
for ($p=1 ;$p <=$pages  ; $p++ )
{
if ($page == $p) {
$direct_bar .= "<b<$p</b< ";
} else {
$direct_bar .= "<a href=\"$PHP_SELF?page=$p\"<$p</a< ";
}
}
}

// Finish off the page
print <<<EOF
<p<$prev $direct_bar $next</p<
EOF;
print $article; // Put article onto page

print <<<EOF
<p<$prev $direct_bar $next</p>
EOF;?>

And that’s it! You should now have three pages instead of one. This really helps to make it all look neat. This same thing can be done with MySQL or any other database just by selecting the article out of the database.

Enjoy!

About the author

Written by Ben Sinclair.

http://www.webmaster-resources101.com Ben Sinclair is the Webmaster of Webmaster Resources 101 (http://www.webmaster-resources101.com/) and Webmaster Forums 101 (http://www.webmaster-forums101.com/)

If you found this post useful you may also want to check these out:

  1. Using SSI’s To Ease Site Maintenance
  2. ASP Web Hosting
  3. Protecting your PHP and HTML Source Code
  4. Cascading Style Sheets (CSS); Learning More
  5. Your First ASP Page
  6. Taming the Update Monster