June 2, 20179 yr Hi all, I have a search results page on my site and was wondering how can I divide search results to 10 per page as some keywords could bring up many links ('bulletin' for example). Here's my site Thanks, Niall
June 3, 20179 yr Hi all, I have a search results page on my site and was wondering how can I divide search results to 10 per page as some keywords could bring up many links ('bulletin' for example). Here's my site Thanks, Niall Research Pagination it involves using a language like php to split up search results into page links. A good PHP class to do just that is here https://github.com/stefangabos/Zebra_Pagination also i'm sure wordpress has a plugin for this as well if you are using wordpress Edited June 3, 20179 yr by webdesigner93
June 3, 20179 yr Depending on the amount of data I normally use a count first followed by an offset call for the 10 per page. $sql_count = "SELECT COUNT(*) FROM `table`"; $stmt = mysqli_prepare($con, $sql_count); if(mysqli_stmt_execute($stmt)) { $result = mysqli_stmt_get_result($stmt); while($row = mysqli_fetch_array($result, MYSQLI_NUM)) { foreach($row as $r){ $total_rows = $r; } } } mysqli_stmt_free_result($stmt); mysqli_stmt_close($stmt); $rowsperpage = 10; // How many results per page. $totalpages = ceil($total_rows / $rowsperpage); if(isset($_GET['page']) && is_numeric($_GET['page'])) { $page = (int)$_GET['page']; $curpage = $page; } else { $page = 1; $curpage = 1; }; $offset = ($page - 1) * $rowsperpage; $result_start = ($curpage-1)*$rowsperpage+1; $result_end = $result_start+$rowsperpage-1; if($result_end >= $total_rows) { $result_end = $resCount; }; if($page > $totalpages && $totalpages>=1) { // I normally forward back to last page e.g. they try page 14 of 13 they forward to 13. }; // now you can get just the 10 required for the page $sql_data = "SELECT `col1`, `col2`, `col3` FROM `table` LIMIT ?, ?"; // these would be $offset, $rowsperpage Then you just need the pagination ... <?php if($totalpages >= 2) { // Only show pagination if there are more than 1 page. $pageurl = "http://www.yourdomain.com/pagename/"; // Used to keep clean pages ?> <div>Page <b><?php echo $page; ?></b> of <b><?php echo $totalpages; ?></b><br> Showing <?php echo $result_start; ?> - <?php echo $result_end; ?> of <?php echo $total_rows; ?> Results</div> <div> <?php $range = 2; // How many page links you want in a range around the current page. for($x = ($page - $range); $x < (($page + $range) + 1); $x++) { // if it's a valid page number... if(($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $page) { // 'highlight' it but don't make a link echo "<span>" . $x . "</span>"; } else { echo "<a href=\"" . $pageurl; if($x>= 2) { // Only add a page link if greater than 1, /pagename/?page=1 is the same results as /pagename/ echo "?page=".$x; } echo "\">".$x."</a>"; } } } ?> </div> <?php } ?> Edited June 3, 20179 yr by BrowserBugs
Create an account or sign in to comment