Giovanni Posted September 8, 2016 Share Posted September 8, 2016 Hello, I am a newbie regarding php, I created a website with a MySql database, I have added pagination. Everything is workign fine. One function I dont get working properly. I want to have the max of pages between the <<prev and the next>> button limited to 10, So like google.. <<prev 1,2,3,4,5,6,7,8,9,10 next>> now its like <<prev 1,2,3,4,5,6,7,8,9,10,11,12,13,14 next>> etc etc. Ill hope someone can help me out. <?php require_once('./include/connect.php'); $page = $_REQUEST["page"]; $amount = 10; $start = $page * $amount; $currentpage = $page + 1; $queryamount = $amount + 1; $query = "SELECT date, id1, id2, id3, id4, id5, id6, id7 FROM member LIMIT $start, $queryamount"; $response = mysqli_query($link, $query); $query2 = "SELECT COUNT(*) as total FROM wotd_id"; $response2 = mysqli_query($link, $query2); $row2 = mysqli_fetch_assoc($response2); $totalpages = ceil($row2['total'] / $amount); if($response){ echo "<center>(You are on Page <b>$currentpage</b> of <b>$totalpages </b>) </center>"; $result = mysqli_query ($link,"select count(1) FROM member"); if($result) { $row = mysqli_fetch_array($result); $total = $row[0]; echo "<center>Currently there are <b>$total</b> entry's in the Database </center>"; } $i = $amount; while($row = mysqli_fetch_array($response)){ $i--; if($i < 0) break; } if($page > 0) { echo "<a href= '" . $_SERVER["PHP_SELF"] . "?page=" . ($page-1) . "' class='button'><b> << Prev</b></a> "; } for($i = 1; $i <= $totalpages; $i++) { //echo "<a href= '" . $_SERVER['PHP_SELF'] . "?page=" . ($i-1) . "'>"; if($i == $currentpage) { echo "<span class=active_page>[" . $i . "]</span>"; } else { //echo "<span class = page_nonactive>" $i "</span>"; echo "<a href= '" . $_SERVER['PHP_SELF'] . "?page=" . ($i-1) . "'>"; echo "<span class=nonactive>" .$i . "</span>"; } echo "</a> "; } for($i = $currentpage + 1; $i <= min($currentpage + 11, $total_pages); $i++) if($currentpage < $totalpages) { echo "<a href='" . $_SERVER["PHP_SELF"] . "?page=" . ($page+1) . "' class='button'><b>Next >> </b></a> "; } } else { echo "Couldn't issue database query"; echo mysqli_error($link); } mysqli_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/302103-pagination-results-between-prev-and-next-button/ Share on other sites More sharing options...
Psycho Posted September 8, 2016 Share Posted September 8, 2016 (edited) An easier way is to set a page link offest. E.g. the pages in the pagination list will be +/- 5 pages from the current page. That means if you are on either end of the total pages one side will be less than the offset. There are quite a few problems and inefficiencies with your code. Here is a quick rewrite - have not tested so there may be some minor errors. Read the comments to understand what is happening. There are a lot of other improvements I would make to this, but did not want to invest the time. <?php //------ Configuration variables -------- $recordsPerPage = 10; //Define the records per page $pageLinkOffset = 5; //Offset for displayed pages //--------------------------------------- //Connect to DB require_once('./include/connect.php'); //Get the total number of records and pages $query = "SELECT COUNT(*) as record_count FROM member"; $result = mysqli_query($link, $query); $row = mysqli_fetch_assoc($result); $totalRecords = $row['record_count']; $totalPages = ceil($totalRecords / $recordsPerPage); //Get the requested page $page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1; //If requested page is not within the range of available pages, set to page 1 if($page<1 || $page>$totalPages) { $page = 1; } //set limit start value and run query to get records $limitStart = ($page-1) * $recordsPerPage; $query = "SELECT date, id1, id2, id3, id4, id5, id6, id7 FROM member LIMIT {$limitStart}, {$recordsPerPage}"; $result = mysqli_query($link, $query); //Create the output $output = ""; $output .= "<center>(You are on Page <b>{$page}</b> of <b>{$totalPages}</b>)</center>\n"; $output .= "<center>Currently there are <b>{$totalRecords}</b> entry's in the Database</center>"; //Iterate through the results to create the output while($row = mysqli_fetch_assoc($result)) { //Do something with $row to create output for each record $output .= ""; } //Create pagination links $paginationLinks = ""; //Ouput variable //Define start/end pages to show in links based on // offset and current page $pageLinkStart = max($page-$pageLinkOffset, 1); $pageLinkEnd = min($page+$pageLinkOffset, $totalPages); //Make the root url for action attribute safe $pageURL = htmlentities($_SERVER['PHP_SELF']); //Create prev page link if($page > 1) { $prevPage = ($page-1); $paginationLinks .= "<a href='{$pageURL}?page={$prevPage}' class='button'><b> << Prev</b></a> \n"; } //Create links for pages for($pageNo = $pageLinkStart; $pageNo <= $pageLinkEnd; $pageNo++) { if($page == $pageNo) { $paginationLinks .= "<span class=active_page>[{$pageNo}]</span> \n"; } else { $paginationLinks .= "<a href='{$pageURL}?page={$pageNo}'></a> \n"; } } //Create next page link if($page < $totalPages) { $nextPage = ($page+1); $paginationLinks .= "<a href= '{$pageURL}?page={$nextPage}' class='button'><b>Next >></b></a> \n"; } mysqli_close($link); ?> <html> <body> <?php echo $output; ?> <br><br> <?php echo $paginationLinks; ?> </body> </html> Edited September 8, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/302103-pagination-results-between-prev-and-next-button/#findComment-1537173 Share on other sites More sharing options...
Giovanni Posted September 8, 2016 Author Share Posted September 8, 2016 wow, I will get into it and will use your rewrite.. I will read more and more to improve my code, as I said I am learning and learning. Thank you very much.. Quote Link to comment https://forums.phpfreaks.com/topic/302103-pagination-results-between-prev-and-next-button/#findComment-1537182 Share on other sites More sharing options...
Psycho Posted September 8, 2016 Share Posted September 8, 2016 My advice, think about what you want to achieve and decide upon a logical path - you can do this as a sort of flowchart using pencil & paper. Then write your code. And use lots of comments! Quote Link to comment https://forums.phpfreaks.com/topic/302103-pagination-results-between-prev-and-next-button/#findComment-1537184 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.