Jump to content

Pagination script displaying blank records


ianh

Recommended Posts

Hi everyone,

 

I'm glad to join this forum.

 

I have a pagination script displaying files from specified directories. The 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. PDF3

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 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> | 

[][/]

Before your for loop of the records:

for($i=0; $i<$number_to_display; $i++) {

You can add this bit of code:

$number_to_display = ($number_to_display > $total_files) ? $total_files : $number_to_display;

 

That way if the number to display (In this case 9) is greater than the total number of files, the number to display is changed to the actual number of files.

Check to see if it's the last page, if it is, display

<?php
$total_files % $number_to_display; // In your example, 3
?>

number of records, instead of just $number_to_display numbers of records.

 

to the poster above: $total_files is the total count of ALL files not the ones for the current page.

Check to see if it's the last page, if it is, display

<?php
$total_files % $number_to_display; // In your example, 3
?>

number of records, instead of just $number_to_display numbers of records.

 

to the poster above: $total_files is the total count of ALL files not the ones for the current page.

 

Hi genericnumber1, how do I check to see if it is the last page? Do you have a code example of this?

Also where within my script should your code above be inserted?

It looks like you're going based upon record to start with instead of page to start with... that will complicated it a bit. You might just consider adding the band-aid fix of...

 

// add to the second line of the loop (after you set $vf)
if(!isset($narray[$vf])) {
   break;
}

 

if you don't want to change it to using a page system instead of your current start-index system.

It looks like you're going based upon record to start with instead of page to start with... that will complicated it a bit. You might just consider adding the band-aid fix of...

 

// add to the second line of the loop (after you set $vf)
if(!isset($narray[$vf])) {
   break;
}

 

if you don't want to change it to using a page system instead of your current start-index system.

 

genericnumber1 that worked great, problem solved, your a star ;D Thank you so much!!

 

I also posted my problem on 2 other forums giving me mostly dudd answers that didn't work. But this forum saved my day!

 

Sticking to PHP Freaks forum from now on ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.