PHP Paging With Custom Pagesize

  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

Hello,

I was going through this tutorial:
http://www.php-mysql-tutorial.com/php-mysql-paging.php

And noticed this line:
Code: [ Select ]
$rowsPerPage = 20;


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
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Assuming the code works properly as-is, you should be able to type-cast the visitors input to an int which will convert non-numeric values to zero, then make sure the value submitted is at least 1 & lower than something sane so you don't have the whole table being returned at once.

Code: [ Select ]
$rowsPerPage = empty($_GET['perpage']) ? 25 : min(50, max(1, (int)$_GET['perpage']));


Past that you could just provide a <select> element with common increments like 5, 10, 25, & 50.

That should weed out any bugs that could arise just by making the value changeable.
  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

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

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 35 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.