if a number is a multiple of another number  for recordset paging

Posted on 2009-04-22
Last Modified: 2012-05-06

I built a recordset paging function.  

i use a few variables such as
$number_per_page = 6;
that is how many records i want to show on every page

another which is important is
this number is a calculation of how many page links to display (eg Page1,2,3,...), i get that number like this...

$query_Recordset_paging = "SELECT * FROM shoes WHERE shoes_brand = $id ORDER BY shoes_name ASC";
$Recordset_paging = mysql_query($query_Recordset_paging, $mine) or die(mysql_error());
$totalRows_Recordset_paging = mysql_num_rows($Recordset_paging);

$amountofpages = round($totalRows_Recordset_paging / $number_per_page  + .5);

Basically i get the total rows and divide it by the $number_per_page and add .5 to round up all the pages

The problem is when i have an amount of pages which is a multiple of $number_per_page for example if $amountofpages = 12, or 18   I will get an extra page which has no results...

i tried the code below but it doesnt help....
if ($amountofpages % $number_per_page   == 0) {

    $amountofpages = ($amountofpages - 1);


Open in new window

Question by:jblayney
    1 Comment
    LVL 39

    Accepted Solution

    Use the ceil() function, it rounds up:

    $amountofpages = ceil($totalRows_Recordset_paging / $number_per_page);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now