Nightasy Posted July 19, 2013 Share Posted July 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/ Share on other sites More sharing options...
mac_gyver Posted July 19, 2013 Share Posted July 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/#findComment-1441435 Share on other sites More sharing options...
AbraCadaver Posted July 19, 2013 Share Posted July 19, 2013 (edited) 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 July 19, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/#findComment-1441436 Share on other sites More sharing options...
Nightasy Posted July 19, 2013 Author Share Posted July 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/#findComment-1441437 Share on other sites More sharing options...
Nightasy Posted July 19, 2013 Author Share Posted July 19, 2013 (edited) Suppose I'll probably have to look into a different route. Not using array_splice is making the page get really long. Edited July 19, 2013 by Nightasy Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/#findComment-1441438 Share on other sites More sharing options...
Solution Nightasy Posted July 19, 2013 Author Solution Share Posted July 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280328-notice-undefined-offset/#findComment-1441441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.