This is how to paginate a long story/article by sentences. This tutorial will teach you how to split large texts into arrays, get rid of the parts not in the page, collapse the parts you want together and finally paginate the parts.
So first I will show you the entire example code, this tutorial does not require any databases.Code:
<?php
$numsent=3; //number of sentences per page
if(!isset($_GET['ID']))
{
$ID=0;
}
else
{
$ID=$_GET['ID'];
}
$string="123.234.345.456.567.678.789.890.111.000"; //test string
$stringparts=explode('.',"$string"); //breaks string into parts
$startnum=$ID*$numsent;
$endnum=$startnum+$numsent;
$numpartsinstring=count($stringparts); //counts number of parts in string
for($i=0;$i<$startnum;$i++)
{
unset($stringparts[$i]); //kills parts before
}
for($j=$endnum;$j<$numpartsinstring;$j++)
{
unset($stringparts[$j]); // kill parts after
}
$newstring=implode($stringparts,'.');
print "$newstring";
print "<br><br>";
$numpages=ceil($numpartsinstring/$numsent);
for($i=1;$i<=$numpages;$i++)
{
$actualstart=$i-1;
if($actualstart!=$ID)
{
print "<A href='test.php?ID=$actualstart'>$i</a> ";
}
else
{
print "$i ";
}
}
?>
So lets go through this code step by step. At the top I declareCode:
$numsent=3;
This is basically saying I want 3 sentences per page. In reality, if you were doing pagination on a long story/article, this would be like 50 for 50 sentences per page or something.
Next I set $ID.Code:
if(!isset($_GET['ID']))
{
$ID=0;
}
else
{
$ID=$_GET['ID'];
}
$ID will be the parameter I accept in the URL to tell me what page I'm on. If there is nothing passed in, I will assume its zero(1st page).
Next I set the test string:Code:
$string="123.234.345.456.567.678.789.890.111.000"; //test string
This is the example string I will be paginating. This string has 9 periods and ten sentences, so it will make for a good example since I will have 4 pages using this string at the rate of 3 sentences per page.
In reality, $string would be the article or story you are trying to parse. You'd probably either read it in from a file or query it in from a database. In this example we are just going to use the simple string.
Then I use the explode function to make the string into an array:Code:
$stringparts=explode('.',"$string"); //breaks string into parts
Now $stringparts holds what was in string in array format. So $stringparts[0] is the first sentence and $stringparts[1] is the second sentence and so on.
Now we define the starting and ending sentences for the page we are on:Code:
$startnum=$ID*$numsent;
$endnum=$startnum+$numsent;
Since $ID is the page we are on and $numsent is the number of sentences per page, it makes sense that our starting sentences would be the page we are on multiplied by the number of sentences per page.
Ending sentence is also set to the starting sentence plus the number of sentences per page. Setting these variables that way will give you $numsent of sentences per page, just like what we want.
Next we want to count how many parts there are in the array or in other words how many sentences are in the entire string. We can do this with this code:Code:
$numpartsinstring=count($stringparts); //counts number of parts in string
Now we get to the meaty part of the tutorial. For this page we need to delete the parts(sentences) of the array before the part we want and after the parts we want. So we do this code:Code:
for($i=0;$i<$startnum;$i++)
{
unset($stringparts[$i]); //kills parts before
}
for($j=$endnum;$j<$numpartsinstring;$j++)
{
unset($stringparts[$j]); // kill parts after
}
Since the first sentence of the array is always the 0th element, we start $i at zero and we have it loop through the starting number minus 1 of the array. We then use
unset() to delete all the elements in that range. This effectively deletes all the sentences in the array before the start of our page.
The second loop works the same way, except we loop from the ending sentence of our page to the end of the array and delete the parts in that range using unset().
After deleting these two chunks, we now have an array with just the text we want for the page, but it also has alot of blank parts. We need to now combine those blank parts into a new string with the implode() function.
So we do this:Code:
$newstring=implode($stringparts,'.');
print "$newstring";
print "<br><br>";
The implode() function simply collapses all the leftover elements of the array together into a new string with the period as the delimiter. The empty parts of the array now have nothing between periods so it just gets rid of those when imploding.
The we simply print out the string and two line breaks.
Finally we have to make the page numbers show up so we do:Code:
$numpages=ceil($numpartsinstring/$numsent);
for($i=1;$i<=$numpages;$i++)
{
$actualstart=$i-1;
if($actualstart!=$ID)
{
print "<A href='test.php?ID=$actualstart'>$i</a> ";
}
else
{
print "$i ";
}
}
?>
First we have to determine the number of pages we have. We do this by dividing the number of sentences in our original string by the number of sentences we want per page. We use ceil() to round up because if we have 3.2 pages, we want it to show the last sentence on the 4th page.
Then we tell $i to start at 1 and go through all the pages we have. We have to set $actualstart to $i-1 because $ID is always one behind $i because when $ID is zero is when we have our 1st page.
Then we check to see if $ID is equal to $actualstart or not. If it isn't, we want it to print a hyperlink for the navigation number because it is not the page we are currently on.
If it is equal, then just print a number as it is the current page we are on.
Thats it for the pagination tutorial.
discuss this topic to forum
