Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Notice: Undefined offset:

$
0
0

So, I'm playing around with pagination. The script I wrote determines how many images are in a folder, throws them into an array and then splits them up so that only 9 are displayed per page.

 

It all works great except for that if a page doesn't have 9 images on it I get this error.

 

Notice: Undefined offset:... blah blah blah

 

I already know that it has to do with the array not having an existing field and I am supposed to use an isset to check that the array field even exists. But everywhere I have tried to put it, it doesn't work and actually causes none of the images to appear and only the errors. If anyone could show me what I'm doing wrong here I'd really appreciate it.

// Create Array
$files = array();
if( is_dir( $thumbs_dir ) ) {
  if( $handle = opendir( $thumbs_dir ) ) {
    while( ( $file = readdir( $handle ) ) !== false ) {
      if( $file != "." && $file != ".." ) {
        array_push( $files, $file );
      }
    }
    closedir( $handle );
  }
}

// Set images per page and determine total pages.
$page_size = 9;
$total_pages = ceil( count( $files ) / $page_size );

// Create divs.
echo '<div id="centerwrapper">';
echo '<div id="left_div">';

// Determine Page and Display.
if( isset( $_GET['p'] ) ) {
  $current_page = $_GET['p'];
  if( $current_page > $total_pages ) {
    $current_page = $total_pages;
  }
} else {
  $current_page = 1;
}

$start = ( $current_page * $page_size ) - $page_size;

// Title Gallery and display page number.
echo $_SESSION['username'] . "'s Images<br>";
echo "Page " . $current_page . " of " . $total_pages . "<br><br>\n\n" ;

// List page selectors.
for( $j=0; $j<$total_pages; $j++ ) {
  $p = $j + 1;
  print( "<a href='?p=" . $p . "'>" . $p . "</a> " );
}

echo "<br><br>";
echo "<hr>";


for( $i=$start; $i<$start + $page_size; $i++ ) {
	
	/*Verify file exists, if exists echo file.*/
	if( is_file( $thumbs_dir . $files[$i] ) ) {
		$thumbnail_image = $thumbs_dir.$file[$i];
		$medium_image = $medium_dir.$file[$i];
		echo '<a class="DDImage" href="'.$medium_image . $files[$i] .'" rel="enlargeimage" rev="targetdiv:loadareatest,trigger:click,preload:none,fx:reveal,link:'.$images_dir . $files[$i] .'"><img class="photo-link" src="'.$thumbnail_image . $files[$i] .'" /></a>';	
  }
}
echo '</div>';
echo '</div>';
echo '<div id="loadareatest"></div>';

Oh and if you see a variable that's not being used it's because this is only part of the entire code that I have planned for this page.

 

Best Regards,

Nightasy


Viewing all articles
Browse latest Browse all 13200

Trending Articles