1

I am creating a page for someone where it will display all orders in the database, limited to 10 per page.

It works to that end, but now the issue is when I try to get a max number of pages using the below code:

$msql = "SELECT * FROM orders";
$mresult = $db->query($msql);
if(is_object($mresult) && $mresult->num_rows > 0)
{
    if($mresult->num_rows > 10)
    {
        $maxpage = $mresult->num_rows / 10 + 1;
    }
    else
    {
        $maxpage = 1;
    }
}

It won't set the $maxpage variable to anything other than 1 unless there are 20 or more orders in the database.

Right now, I have 11 test orders in the database, so when it divides by 10, it should set max pages to 1, + 1 additional page since there are more than 10 orders.

I have scoured the web looking for a solution, and have came up empty handed. Any thoughts or suggestions?

EDIT:

Added code at bottom of the table the actual orders are displayed for further troubleshooting:

<tr>
    <td colspan="2" align="left"><a href="orders?start=<?php if($page == 1) { echo 0; } else { echo $start - 10; } ?>&page=<?php if($page == 1) { echo 1; } else { echo $page - 1; } ?>">&lt;&lt;&lt;&lt;</a> Prev</td>
    <td align="center">Page <?php if(isset($_GET['page'])) { echo $_GET['page'] . ' of ' . $maxpage; } else { echo '1 of 1'; } ?></td>
    <td colspan="2" align="right">Next <a href="orders?start=<?php if($page <= $maxpage) { echo 0; } else { echo $start + 10; } ?>&page=<?php if($page < $maxpage) { echo $page + 1; } else { echo $page; } ?>">&gt;&gt;&gt;&gt;</a></td>
</tr>
Bryan
  • 59
  • 1
  • 7
  • `(11 / 10 + 1)` is `2` in PHP as far as I know. I actually don't see anything wrong with your logic. – Tim Biegeleisen Nov 19 '16 at 11:51
  • Possible duplicate of [Find total number of results in mySQL query with offset+limit](http://stackoverflow.com/questions/5928611/find-total-number-of-results-in-mysql-query-with-offsetlimit) – CBroe Nov 19 '16 at 12:24

3 Answers3

0

Your code is running fine, the problem is your statement, the statement where you are dividing by 10, should be within parenthesis, because that needs to be computed first, then add 1 to it, in your code, addition is being done which equals 11, and then 11/11 = 1; you probably should wrap it within floor function as well.

if($mresult->num_rows > 10)
{
    $maxpage = (floor($mresult->num_rows / 10)) + 1;
}
else
{
    $maxpage = 1;
}
Talha Abrar
  • 880
  • 8
  • 22
  • Tried your answer and still having same issue. I'm starting to think the issue may lay in the navigation section now at bottom of the table where the orders are actually listed. I have edited my original post to show that code. – Bryan Nov 19 '16 at 12:08
0

Please use below code, I have tested.

$query = "select * from orders";
$history_res = mysql_query($query);

//count records
$totrecords = mysql_num_rows($history_res);
$totpages = ceil($totrecords / 10);
return $totpages;
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
0

Implementing the code provided by @Talha, and changing my navigation controls to the following fixed the issue:

<tr>
    <td colspan="2" align="left"><a href="orders?start=<?php if($page == 1) { echo 0; } else { echo $start - 10; } ?>&page=<?php if($page == 1) { echo 1; } else { echo $page - 1; } ?>">&lt;&lt;&lt;&lt;</a> Prev</td>
    <td align="center">Page <?php echo $page . ' of ' . $maxpage; ?></td>
    <td colspan="2" align="right">Next <a href="orders?start=<?php if($page < $maxpage) { echo $start + 10; } else { echo $start; } ?>&page=<?php if($page < $maxpage) { echo $page + 1; } else { echo $page; } ?>">&gt;&gt;&gt;&gt;</a></td>
</tr>
Bryan
  • 59
  • 1
  • 7