I would like to modify the script below so that there are no pagination numbers, just a previous and next button. I would like the previous/next links to show/be grayed out depending on whether the next/previous page exists. For example, if you are on page 1, no previous button would show because page 0 doesn’t exist.
The script I’m using at the moment grabs data from a text file – creating dynamic pages – and creates pagination based on that:
(before start of html)
$data=file("brief.txt");
$pages=0;
foreach($data as $temp){
$x=explode("|",$temp);
if($x[0]>0){
$pages=$pages+1;
}
}
if($_GET['p']){
$page=$_GET['p'];
}
if($_GET['i']){
$index=$_GET['i'];
}
if($index == "p"){
$page=$page-1;
}
if($index == "n"){
$page=$page+1;
}
if($page < 1){
$page=1;
}
if($page > $pages){
$page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);
?>
(in body section)
<?php
$show=6;
echo "<li><a href='?i=p&p=$page'>« PREV</li></a>";
if($page-($show/2) > 1){
$temp=$page-$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
if($page-($show/2) >= 1 && $page+($show/2) <= $pages){
$start=$page-($show/2);
$stop=$page+($show/2);
}
if($page-($show/2) < 1){
$start=1;
$stop=$show;
}
if($page+($show/2) > $pages){
$start=$pages-$show;
$stop=$pages;
}
for($i=$start; $i<=$stop; $i++){
if($page==$i){
echo "<li class='active'>$i</li></a>";
}
else{
echo "<li><a href='?p=$i'>$i</li></a>";
}
}
if($page+($show/2) < $pages){
$temp=$page+$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
echo "<li><a href='?i=n&p=$page'>NEXT »</li></a>";
?>
I’ve gotten just the next and previous buttons to show and work from simply changing the script to this:
<?php
echo "<li><a href='?i=p&p=$page'>« PREV</li></a>";
echo "<li><a href='?i=n&p=$page'>NEXT »</li></a>";
?>
But I don’t know where to go from there. Any help would be awesome! 🙂
For one thing, the nesting of the tags is wrong – </a> should come before </li>. Secondly, the code isn’t very elegant – it could be much shorter.
But to answer your question about disabling the links, you could just use
(your total number of pages can be found in $pages).