Jump to content

File list pagination: Undefined offset


eevan79

Recommended Posts

I am getting following notice in code:

Notice:  Undefined offset:  2 in ...

Notice:  Undefined offset: 3 in etc..

 

I want to get all files from directory with pagination system. It is working, but how to remove this notice (when error_reportings is enabled). Here is code:

 

    $exts = array('w3g');
    $per_page = 10; // How many files per page
    
    $files = array();
    $dir = opendir($replayLocation);
    while( ($file = readdir($dir)) != false )
    {
        if( !is_dir($file) && !in_array($file,array('.','..')) && in_array(substr($file,strrpos($file,'.')+1),$exts) )
        {
            $files[] = array(
                    'path' => $file,
                    'filename' => pathinfo($file,PATHINFO_BASENAME),
                    'name' => pathinfo($file,PATHINFO_FILENAME)
                );
        }
    }
    closedir($dir);
    
    $file_count = count($files);
    $page_count = ceil( $file_count / $per_page );
    $page = ( isset($_GET['page']) && is_numeric($_GET['page']) ) ? (int) $_GET['page'] : 1;
    if( $page > $page_count )
    {
        $page = $page_count;
    }
    if( $page < 1 )
    {
        $page = 1;
    }

    $offset = ( ( $page - 1 ) * $per_page );
    
    
    for( $i = $offset; $i < ( $offset + $per_page ); $i++ )
    {
        //$files[$i]['path'];
        //$files[$i]['filename'];
       if (file_exists($replayLocation."/".$files[$i]['filename']))
        {
        $current_file = $files[$i]['filename'];
        echo "<a href='./$replayLocation/$current_file'>$current_file</a><br>";
        } 
    }
    
    echo '<br><table><tr><td style="padding-right:24px;" align="right" class="pagination">';
    if( $page > 1 )
    {
        echo ' <a href="./file.php?page=1"><<</a> <a href="./file.php?page=',( $page - 1 ),'"><</a> ';
    }
    $range = 5;
    for( $x = ($page - $range); $x < ( ($page + $range) + 1); $x++ )
    {
        if( $x > 0 && $x <= $page_count )
        {
            if( $x == $page )
            {
                echo ' [ ',$x,' ] ';
            }
            else
            {
                echo ' <a href="./file.php?page=',$x,'">',$x,'</a> ';
            }
        }
    }
    if( $page != $page_count )
    {
        echo ' <a href="./file.php?page=',( $page + 1 ),'">></a> <a href="./file.php?page=',$page_count,'">>></a> ';
    }
    echo '</td></tr></table>';

Link to comment
https://forums.phpfreaks.com/topic/216712-file-list-pagination-undefined-offset/
Share on other sites

I'm sorry, but I thought it was obvious that it is in loop.

It shows that it is in line 50 and 52 which is:

if (file_exists($replayLocation."/".$files[$i]['filename']))

        {

      $current_file = $files[$i]['filename'];

Complete code:

$exts = array('w3g');

    $per_page = 10; // How many files per page

   

    $files = array();

    $dir = opendir($replayLocation);

    while( ($file = readdir($dir)) != false )

    {

        if( !is_dir($file) && !in_array($file,array('.','..')) && in_array(substr($file,strrpos($file,'.')+1),$exts) )

        {

            $files[] = array(

                    'path' => $file,

                    'filename' => pathinfo($file,PATHINFO_BASENAME),

                    'name' => pathinfo($file,PATHINFO_FILENAME)

                );

        }

    }

    closedir($dir);

   

    $file_count = count($files);

    $page_count = ceil( $file_count / $per_page );

    $page = ( isset($_GET['page']) && is_numeric($_GET['page']) ) ? (int) $_GET['page'] : 1;

    if( $page > $page_count )

    {

        $page = $page_count;

    }

    if( $page < 1 )

    {

        $page = 1;

    }

 

    $offset = ( ( $page - 1 ) * $per_page );

   

   

    for( $i = $offset; $i < ( $offset + $per_page ); $i++ )

    {

        //$files[$i]['path'];

        //$files[$i]['filename'];

      if (file_exists($replayLocation."/".$files[$i]['filename']))

        {

        $current_file = $files[$i]['filename'];

        echo "<a href='./$replayLocation/$current_file'>$current_file</a><br>";

        }

    }

   

    echo '<br><table><tr><td style="padding-right:24px;" align="right" class="pagination">';

    if( $page > 1 )

    {

        echo ' <a href="./file.php?page=1"><<</a> <a href="./file.php?page=',( $page - 1 ),'"><</a> ';

    }

    $range = 5;

    for( $x = ($page - $range); $x < ( ($page + $range) + 1); $x++ )

    {

        if( $x > 0 && $x <= $page_count )

        {

            if( $x == $page )

            {

                echo ' [ ',$x,' ] ';

            }

            else

            {

                echo ' <a href="./file.php?page=',$x,'">',$x,'</a> ';

            }

        }

    }

    if( $page != $page_count )

    {

        echo ' <a href="./file.php?page=',( $page + 1 ),'">></a> <a href="./file.php?page=',$page_count,'">>></a> ';

    }

    echo '</td></tr></table>';

 

Errors highlighted in red.

 

Also, I understand why this throws an error. Error occurs when there is more definition in the array, and these files donot exist (for example, when I go to the last page where it displays 3/ 10 results per page - the other 7 files were undefined). How to fix it?

i guess that when there is not enought files to fill a page you get the error.

 

also in simple mode: if there is less then 10 files to display per page you get the error.

 

for example if there are only 5 files insted of 10 in your database.

 

so you have to add an if statement... if(count($files)<10){$per_page=count($files);}

Ok, I solve it. First I must find records per page. And limit it if there is less than total files per page.

 

    $total_on_page = $per_page-(($page*$per_page) - $file_count);

    if ($total_on_page<$per_page) {$per_page = $total_on_page;}

 

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.