Jump to content

Notice: Undefined offset:


Nightasy
Go to solution Solved by Nightasy,

Recommended Posts

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

Link to comment
Share on other sites

if you use array_slice(), it will return the portion of your master array that you want. you can then just loop over that portion of the array using a foreach() loop. the offset and length parameters for array_slice() should be your existing $start and $page_size values.

Link to comment
Share on other sites

I can't tell, especially without errors and line numbers, but I would probably use this approach (much shorter):

$page_size = 9;
$files = glob($thumbs_dir);
$pages = array_chunk($files, $page_size);
$total_pages = count($pages);
$page_range = range(1, $total_pages);
$current_page = isset($_GET['p']) && in_array($_GET['p'], $page_range) ? $_GET['p'] : 1;
$page_items = isset($pages[$current_page-1]) ? $pages[$current_page-1] : $pages[0];

foreach($page_range as $p) {
    //use $p in link
}

foreach($page_items as $file) {
    //use $file
}

I had already typed this before seeing mac_gyver's post, so yes array_slice would work well also.

Edited by AbraCadaver
Link to comment
Share on other sites

if you use array_slice(), it will return the portion of your master array that you want. you can then just loop over that portion of the array using a foreach() loop. the offset and length parameters for array_slice() should be your existing $start and $page_size values.

 

eh, I'm not even familiar with using an array_slice as I'm still learning. Not sure how to even go about doing that.

Link to comment
Share on other sites

  • Solution

I managed to solve this problem by changing the if validation entirely to an isset.

if( isset($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>';	
  }

Works fine now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.