Pagination for nested loops

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
Hi,
Assuming you are outputing all pages:

1) Create functions beforePage() and afterPage() which will start and end a page. At the very least, afterPage() will need to do a 'page throw', whatever that is for you. For an html situation, beforePage() might start a <table> and afterPage() would end it.

2) Create and initialise a variable called $iLine at the top of your code somewhere.
Code:
$iLine = 0;

3) Change the innermost loop to call the functions like this:
Code:
for($f=0; $f<$length6; $f++){
   // New code:
   if ($iLine > $my_lines_per_page) {
      afterPage(); // End the page   	
      $iLine = 0;	
   }
   if ($iLine == 0) {
      beforePage(); // start new page
   }
   $iLine++;	// current line number in page
   // Old code:
   echo '<li>', $list1[$a], ' ', $list2[$b], ' ', $list3[$c], ' ', $list4[$d], ' ', $list5[$e], ' ', $list6[$f], '</li>';
   $counter++;
   if($counter === $limit) break 2;
}
4) you may want to call afterPage() at the end to clean up the last page e.g.:
Code:
   if ($iLine > 0) {
	afterPage(); // End last page   	
   }
-A
 
Back
Top