TomT Posted June 25, 2012 Share Posted June 25, 2012 I have a simple foreach loop foreach ($group as $key => $val) { echo $val . "<br/>"; } This can return upto 500 entries. Is there any simple way to show 25 or 50 on a page with next page / previous page links ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/ Share on other sites More sharing options...
The Little Guy Posted June 25, 2012 Share Posted June 25, 2012 if you are using a database, you could use this: http://phpsnips.com/snip-94 Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/#findComment-1356830 Share on other sites More sharing options...
TomT Posted June 25, 2012 Author Share Posted June 25, 2012 Hi I'm not using a database. The $group is built from a search of files across multiple locations. Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/#findComment-1356832 Share on other sites More sharing options...
PFMaBiSmAd Posted June 25, 2012 Share Posted June 25, 2012 You would do the same as in a database pagination script, except the total number of rows is the count of your array entries, and instead of making a LIMIT clause in a query to define which rows are retrieved and displayed, you calculate the starting and ending array indexes. Simple pagination script, modified to use an array as the data source - <?php // pagination from db -> array $arr = range(1,32); // simulate data (index starts at 0) $rows_per_page = 15; // get total number of rows /* $query = "SELECT count(*) FROM table WHERE ..."; $result = mysql_query($query, $db); list($numrows) = mysql_fetch_row($result); */ $numrows = count($arr); // Calculate number of $lastpage $lastpage = ceil($numrows/$rows_per_page); // condition inputs/set default if (isset($_GET['pageno'])) { $pageno = $_GET['pageno']; } else { $pageno = 1; } // validate/limit requested $pageno $pageno = (int)$pageno; if ($pageno > $lastpage) { $pageno = $lastpage; } if ($pageno < 1) { $pageno = 1; } // Find start and end array index that corresponds to the reuqeted pageno $start = ($pageno - 1) * $rows_per_page; $end = $start + $rows_per_page -1; // limit $end to highest array index if($end > $numrows - 1){ $end = $numrows - 1; } // database query /* $query = "SELECT * FROM table $limit"; $result = mysql_query($query, $db); //... process contents of $result ... */ // display array from $start to $end for($i = $start;$i <= $end;$i++){ echo $arr[$i] . '<br />'; } echo "<br />"; // first/prev pagination hyperlinks if ($pageno == 1) { echo " FIRST PREV "; } else { echo " <a href='?pageno=1'>FIRST</a> "; $prevpage = $pageno-1; echo " <a href='?pageno=$prevpage'>PREV</a> "; } // Display current page or pages echo " ( Page $pageno of $lastpage ) "; // next/last pagination hyperlinks if ($pageno == $lastpage) { echo " NEXT LAST "; } else { $nextpage = $pageno+1; echo " <a href='?pageno=$nextpage'>NEXT</a> "; echo " <a href='?pageno=$lastpage'>LAST</a> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/#findComment-1356859 Share on other sites More sharing options...
TomT Posted June 25, 2012 Author Share Posted June 25, 2012 Thanks that works well apart from one issue. My array isn't [0] => Val, [1] => val1, [2] => val2 etc the key is a filectime, how do I access that using $group[$i] ??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/#findComment-1356863 Share on other sites More sharing options...
kicken Posted June 25, 2012 Share Posted June 25, 2012 Use array_slice to extract the sub section you want, then use that in the foreach loop. $page = array_slice($arr, $start, $numPerPage, true); foreach ($page as $key=>$value){ ... } Quote Link to comment https://forums.phpfreaks.com/topic/264739-foreach-loop-paginate/#findComment-1356870 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.