Search
Sponsors

Posts Tagged ‘PHP’

Adding previous and next to a file

Thursday, February 21st, 2008

by Hal

The following script will break a file up into x sections and show each one with the aid of next and pervious links. To test this script, create a file called ‘file.txt’ and enter test1|test2|test3 10 times on 10 lines. The script should show only the first 2 lines at first, click next would show the next 2 etc.

<?php

$page=$_GET['page'];

/*begins at page one unless another page number is set*/
if(!isset($HTTP_GET_VARS['page']))
{
$page = 1;
}

/*file address put into an array*/
$array = file(”file.txt”);

$s=sizeof($array);

/*number of lines to show per page*/
$show_per_page = 2;

$start = ($page * $show_per_page) - $show_per_page;

/*cutting the array up into sections (pages)*/
$slice = array_slice($array, $start, $show_per_page);

$end=$show_per_page*$page;

/*creating the next and pervious links. If next or previous doesn’t exist, no link is shown*/
if ($start != ‘0′) {
$new_page=$page-1;
$previous=”<a href=’test.php?page=$new_page’>Previous</a>”;
}
else {
$previous=”";
}
if ($end < $s) {
$new_page1=$page+1;
$next=”<a href=’test.php?page=$new_page1′>Next</a>”;
}
else {
$next=”";
}

/*decorating the links*/
echo “<table width=100% align=’center’><tr><td width=50%>$previous</td><td align=right width=50%>$next</td></tr></table>”;

/*the spacer which seperates the sections*/
$break=”|”;

/*looping through and echoing the file parts one by one*/
foreach ($slice as $thisline) {
list($one,$two,$three)=explode($break,$thisline);
echo”$one $two $three”;
}

?>

This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License.

Translate