Hello, I was going through this tutorial: http://www.php-mysql-tutorial.com/php-mysql-paging.php And noticed this line: $rowsPerPage = 20; Code (markup): Here, we are defining the Rows per page in the code itself. I was wondering, if I allow my visitors to set this value from the webpage itself, what bugs would that carry in my code (assuming I am using the exact code in the link above)? Thanx
ok, try this first the form to post rows per page <form method="POST" action="<?echo$_SERVER['PHP_SELF'];?>"> Results per page: <input type="text" name="rowsperpage" maxlength="3" value=""> <input type="submit" value="Show Results"> HTML: second the php code to get these numbers if(isset($_POST["rowsperpage"])) { $rowsperpage=$_POST["rowsperpage"]; } else { if(isset($_GET["r"])) { $rowsperpage=$_GET["r"]; } else { $rowsperpage="20"; } } PHP: now r should be added at the end of your link as $rowsperpage value; $nextpage = " <a href=\"$_SERVER['PHP_SELF']?page=$page&r=$rowsperpage\">Next Page</a> "; PHP:
If you choose any value other than 20, you will need to construct the SQL statement accordingly. Eg: If your per page no. is 5, then the Y(duration) of your SQL LIMIT should be 5 $sql="SELECT * FROM table LIMIT 0,5"; //or $sql="SELECT * FROM table LIMIT 0,$per_page"; PHP:
The reason why I asked u this question was, I currently have 8 records in my DB. The navigation/Pagination works fine if my customer enters any value in the PageSize textbox except the value 1, 6 and 7. 1) If they enter the value 1 in the PageSize textbox then the nav pagination loops from 0 to 7. Clicking on the [7th] hyperlink, gives a record not found error. 2) If they enter the value 6, then the page displays only 6 rows. (Not sure why) 3) If they enter the value 7, then the the page displays only 7 rows. (Not sure why) For the remaining values, it displays all the 8 records. Which is fine. Any idea why is this happening? Thanx
try without for loop if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page&r=$r\">[Prev]</a> "; $first = " <a href=\"$self?page=1&r=$r\">[First Page]</a> "; } else { $prev = ' [Prev] '; // we're on page one $first = ' [First Page] '; // we're on page one } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page&r=$r\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage&r=$r\">[Last Page]</a> "; } else { $next = ' [Next] '; // we're on the last page $last = ' [Last Page] '; // we're on the last page } // print the page navigation link echo "<p align=center>" , $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages" . $next . $last , "</p>"; PHP: