Jump to content

Array Paging help


Kelset

Recommended Posts

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 works
but the main problem is when the "Next" link gets to the end of the array. It just shows blank spots where images should
be (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 value
if ($_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 &nbsp;&nbsp;&nbsp;</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 to
figure 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
Link to comment
Share on other sites

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]
<?php
function 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 show

for ($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.
Link to comment
Share on other sites

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 it
before 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 I
hope I can be of some help to some of you someday.

Stpehen
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.