Further to my post Remove file extensions from array ? I am now paging files from specified directories on my server.
The pagination script works ok except it always displays 9 records no matter what. So if a directory contains just 3 files, the pagination script will add 6 blank records to make a total of 9 records on each page. For example:
1. PDF1
2. PDF2
3. PDF2
4. (blank record)
5. (blank record)
6. (blank record)
7. (blank record)
8. (blank record)
9. (blank record)
I realise the problem is being caused by the string $number_to_display in my code but not sure how this can be adjusted for when there should be less than 9 records displayed on a page?
Any ideas on how the issue can be fixed would be much appreciated!
Here is my code:
<?php
$str_subcat = $_GET['subcat'];
global $str_subcat;
if(!$_GET['start']) {
$start = 0;
} else {
$start = $_GET['start'];
}
$number_to_display = '9';
// The path that we're interested in
$Folder = "pdfs/$str_subcat/";
$narray=array();
$i=0;
// Open the folder
$DirHandle = @opendir($Folder) or die($Folder." could not be opened.");
while($file = readdir($DirHandle))
{
if(is_dir($file))
{
continue;
}
else if($file != '.' && $file != '..' && strpos($file, '.pdf'))
{
//echo "<a href='$path/$file'>$file</a><br/>";
$narray[$i]=$file;
$i++;
}
}
sort($narray);
// Close the handle to the directory
closedir($DirHandle);
$total_files = count($narray);
$req_pages = ceil($total_files/$number_to_display);
echo "total files = ". $total_files."<br>";
for($i=0; $i<$number_to_display; $i++) {
$vf = $i+$start;
echo "<tr>";
echo "<td width=175 class=bodycopy>";
echo "<a href='".$Folder.$narray[$vf].$file."' target=blank>".str_replace(".pdf", "", $narray[$vf].$file)."</a>";
echo "</td>";
echo "<td width=20> </td>";
echo "<td width=35 align=left>";
echo "<a href='".$Folder.$narray[$vf].$file."' onMouseOver=MM_swapImage('Image".$vf."','','http://www.resolutionmag.com/images1/arrow1.jpg',1); onMouseOut=MM_swapImgRestore(); target=blank><img src=http://www.resolutionmag.com/images1/arrow.gif name=Image".$vf." width=15 height=15 border=0></a>";
echo "</td>";
echo "<td width=304> </td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=4> </td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan=4 class=bodycopy>Req Pages = ".$req_pages."</td>";
echo "</tr>";
echo "<tr><td colspan=4 class=bodycopy>";
echo "<a href=\"?subcat=".$str_subcat."&start=0\">First</a> |";
for($x=0; $x<$req_pages; $x++) { ?>
<a href="?subcat=<? echo $str_subcat; ?>&start=<? echo $x*$number_to_display; ?>"><? echo $x+1; ?></a> |
<? } ?>
<a href="?subcat=<? echo $str_subcat; ?>&start=<? echo ($x-1)*$number_to_display; ?>">Last</a>