First, you'll need to be able to start looping from any position in the arrays.
Of course you can use for
loops, but I think while
loops fit better here.
$length1 = count($list1);
$length2 = count($list2);
//now indexes are initialized to variable values
$a = $var1; //start position $var1 is an integer variable between 0 and ($length1 - 1)
$b = $var2; //start position $var2 is an integer variable between 0 and ($length2 - 1)
while ($a < $length1) {
while ($b < $length2) {
echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
$b++;
}
$b = 0; //reset inner loop each time it ends
$a++;
}
Next, we need a way to stop both loops if the maximum number of results per page ($limit
) is reached before the end of the combinations.
$length1 = count($list1);
$length2 = count($list2);
$a = $var1;
$b = $var2;
$counter = 0;
while ($a < $length1) {
while ($b < $length2) {
echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
$counter++;
if($counter === $limit) break 2;
$b++;
}
$b = 0;
$a++;
}
Finally, we must find the correct values for $var1
and $var2
above, based on the current $page
(starting from page 1) and $limit
. This is plain arithmetics that I won't explain here.
Putting it all together:
$length1 = count($list1);
$length2 = count($list2);
$offset = $limit * ($page - 1);
$a = (int)($offset / $length2);
$b = $offset % $length2;
$counter = 0;
while ($a < $length1) {
while ($b < $length2) {
echo '<li>', $list1[$a], ' ', $list2[$b], '</li>';
$counter++;
if($counter === $limit) break 2;
$b++;
}
$b = 0;
$a++;
}