1
<?php
for ($i = 1; $i<=$pages_item; $i++) {
echo "<tr><td>Page ".$i."</td></tr>";   
}
foreach ($array_item as $item) {
echo "<tr class=link onclick=window.location.href='item.php?
id=".$item[$item_id]."'>";
}
?>

I am creating pages from a MySQL query in PHP with CSS instead of javascript. The page system works.

What I need is to be able to limit the foreach loop, like for every page in the for loop, I want a foreach loop to go through my array at a specific start and end point.

I would like to have it like this

for every page do

for every item start here and ending here do

do magic

increase start here

increase ending here

done

I need to have it display 50 results per page from the foreach loop within the for loop. I've seen array slicing but that will only set a start point and doesn't really do what I want.

knuxyl
  • 129
  • 3
  • 12
  • [`array_slice`](http://php.net/manual/en/function.array-slice.php) sets both the offset and length. Can you show what you've tried so we get an idea of what you're doing? Maybe another approach would be more effective, like using MySQL's `LIMIT`. – showdev Mar 27 '17 at 18:01
  • I gave really the only code needed to do this. My query can get up to 5000 results, I don't want to over complicate the query process. I have the results stored in the array_item variable. – knuxyl Mar 27 '17 at 18:15
  • I don't see the part where you tried array slicing. (You might find this interesting: [Best way to implement paging?](http://stackoverflow.com/questions/3799193/mysql-data-best-way-to-implement-paging)) – showdev Mar 27 '17 at 18:24
  • I just read about array slicing, I didn't try it because I didn't know you could set the start and limit. – knuxyl Mar 27 '17 at 19:27

0 Answers0