Pagination (files) script displaying blank records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Pagination (files) script displaying blank records

Post by ianhman »

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

Code: Select all

 
                    <?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>&nbsp;</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>&nbsp;</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td colspan=4>&nbsp;</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> | 
 
User avatar
omika
Forum Newbie
Posts: 19
Joined: Sun Oct 12, 2008 2:00 pm
Location: New Zealand

Re: Pagination (files) script displaying blank records

Post by omika »

You need to somehow incorporate the total amount of files into the for.

Try Changing:

Code: Select all

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

Code: Select all

for($i=0; $i<$number_to_display && $i<$total_files; $i++) {
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Re: Pagination (files) script displaying blank records

Post by ianhman »

omika wrote:You need to somehow incorporate the total amount of files into the for.

Try Changing:

Code: Select all

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

Code: Select all

for($i=0; $i<$number_to_display && $i<$total_files; $i++) {
Hi Omika, I tried your code, sorry but it didn't seem to work, gives the same output as before. Any other ideas?
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Re: Pagination (files) script displaying blank records

Post by ianhman »

I finally got it to work from a response on a different PHP forum.

But thanks anyway, I appreciate it.

Code:

Code: Select all

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