Final Script
Now we should have a fully functional navigation system. You might want to add some extra characters to enhance the appearace - I'll put a few in the final script. Note that everything in this tutorial is in between the stars and the rest is notepad's original script. If you've followed along, below is the complete script. I hope you've found it useful. Hopefully, the next tutorial will cover how to modify the news system to make it a more secure, dynamic, truly useful system.
<?php
// if a page isn't defined, we're on page one
if($page <= 0)
{
$page = 1;
}
// create an array of data
$myArray = file("data.txt");
// reverse the order of the data
$myArray = array_reverse($myArray);
// how many lines of data to display
$display = 5;
// where to start depending on what page we're viewing
$start = ($page * $display) - $display;
/*********************************************************/
$realstart = ($start + 1);
$finish = ($start + $display);
$keys = count($data);
$newspages = ($keys / 10);
if ($finish > $keys) {
$finish = $keys; }
if ($page > 1) {
?>
<< <a href="<?=$PHP_SELF;?>?page=<?=($page - 1);?>">previous</a> ||
<?php }
echo "displaying items " . $realstart . " through " . $finish;
if ($page < $newspages) {
?>
|| <a href="<?=$PHP_SELF;?>?page=<?=($page + 1 );?>">next</a> >>
<?php }
echo "<br />" . $keys . " items total";
/*********************************************************/
// the actual news we're going to print
$news = array_slice($myArray, $start, $display);
// printing the data
foreach($news as $key=>$value)
{
print("line $key: $value<br>\n");
}
?>
Source:
http://codewalkers.com/tutorials/20/5.html