Kelset Posted November 1, 2006 Share Posted November 1, 2006 I have images stored in a directory that I want to display. I wrote the script that will make a array of the image names.The problem I am having is paging through the array to show 10 images at a time. I have wrote some code that worksbut the main problem is when the "Next" link gets to the end of the array. It just shows blank spots where images shouldbe (blank image HTML tag) and it goes on forever.---- Code (No laughter please :( ) --//count the number of picture in the array$num_pic = count($image);//Set number of pictures to show per page$display_num = 10;//Get current page valueif ($_GET['page'] != 0) { $page_num = $_GET['page'];}else{ //Default start page $page_num = 0;}//Get the start number for the array looping$start_pic = $page_num * 10;//Get the end number for array looping$end_pic = $start_pic + 10;//Set row count for table$row_count = 0;echo"<TABLE border ='1'><TR>\r";for ($i = $start_pic; $i<$end_pic;$i++) { //Check row_count to see if there should be a new table row made or and col. if ($row_count < 3) { echo"<TD valign=\"top\"><img src=\"photos/bethchris/".$image[$i]."\" alt=\"\">"; echo"<br><INPUT TYPE=\"checkbox\" NAME=\"".$image[$i]."\"><div class=\"title\">Check to order</div> </TD>\r"; //Increment row_count by 1 $row_count++; }else{ echo"<TD valign=\"top\"><img src=\"photos/bethchris/".$image[$i]."\" alt=\"\">"; echo"<br><INPUT TYPE=\"checkbox\" NAME=\"".$image[$i]."\"><div class=\"title\">Check to order</div> </TD>\r"; echo"</TR><tr>\r"; //Re-set the row_count to start new col. $row_count = 0; }} if ($page_num == 0) { //On first page so there is not previous link needed //Set value for next page. $next_page = $page_num +1; echo"<tr><td colspan='4'><a href='display.php?page=".$next_page."'>Next</a><p></td></tr>\r"; }else{ //Fine out what page we are on and create the proper Next and Previous links $pre_page = $page_num - 1; $next_page = $page_num +1; echo "<tr>/r<td colspan='4' align='center'><a href='display.php?page=".$pre_page."'>Previous </a><a href='display.php?page=".$next_page."'>Next</a><p><input type=\"submit\" value=\"Order Photos\" id=\"submit\"></td>\r</tr>"; } echo "</TABLE>"; ?>Any help would be great, I have been searching for array paging but I only get the MySQL paging results one. I guess I need tofigure out how to stop loop once it run to the end. maybe an if statment using isset(array) or something like that.Thank you for you time in this for me Stephen Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 1, 2006 Share Posted November 1, 2006 The principles behind paging through an array and paging through MySQL results will be almost identical. Here's one idea of how to do it:[code]<?phpfunction primeIt() { $x = array(); for ($i = 1; $i < 101; $i++) $x[] = $i; return $x;}$array = primeIt(); // this is just giving me an array with 100 items$p = isset($_GET['p']) ? $_GET['p'] : 1; // default to page 1$limit = 10; // show 10 results per page$from = ($limit * $p) - $limit; // first item in array to show$to = $from + $limit; // last item in array to showfor ($i = $from; $i < $to; $i++) { echo $array[$i] . '<br />';}?>[/code]Now, all you have to do to get the above code to display different pieces of the array is add [i]?p=X[/i] where [i]X[/i] is your page number you want to display. Hope this helps. Quote Link to comment Share on other sites More sharing options...
Kelset Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks your code helped me to find out what my problem was. I was not doing testing for the next link, I should have thought of itbefore hand. All I had to do is see if array[$i] had no value if it didn't then show only the previous link. Again thanks for the help and Ihope I can be of some help to some of you someday.Stpehen Quote Link to comment 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.