<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Comments on snippet: 'Pagination Madness'</title>
    <description>Snipplr comments feed</description>
    <link>https://snipplr.com/</link>
    <lastBuildDate>Sun, 19 Apr 2026 00:47:14 +0000</lastBuildDate>
    <item>
      <title>iroybot said on 20/Feb/2009</title>
      <link>https://snipplr.com/view/12077/pagination-madness</link>
      <description>&lt;p&gt;Let's say we want to show 4 items per page. Let's calculate this through:&#13;
&#13;
first case: we have 8 items. 8 divided by 4 is 2 so we need two pages. good&#13;
&#13;
second case: we have 9 items. 9 divided by 4 gives us 2.25.&#13;
&#13;
So we clearly need a 3rd page here. That's why I compare two values here: the actual division result and one with an extra  0.499 added what gives us 2.749. (lines 2-4)&#13;
lines 5-6 we round both results in order to find out if they get rounded to different integers.&#13;
if they differ by 1 we have a scenario where we want another page, so we pick the bigger result as our page number. (lines 7-8). if they get rounded to the same integer we are happy and use the smaller number.&#13;
&#13;
note: if we added 0.5 instead of 0.499 the first case would give us 2.5 which would have been rounded to 3. but actually we are fine with 2 pages for 8 items.&#13;
&#13;
now that we know our actual page count we can do something like&#13;
&#13;
while($i = 1;$i &lt;= $pages; $i++) { print '&lt;a href="?page=' . $i . '"&gt;'; }&lt;/p&gt;</description>
      <pubDate>Fri, 20 Feb 2009 20:23:08 UTC</pubDate>
      <guid>https://snipplr.com/view/12077/pagination-madness</guid>
    </item>
    <item>
      <title>iroybot said on 24/Apr/2009</title>
      <link>https://snipplr.com/view/12077/pagination-madness</link>
      <description>&lt;p&gt;shame on me... use PHPs native ceil() function (instead of this crazy +.499 guessing, I did...)&lt;/p&gt;</description>
      <pubDate>Fri, 24 Apr 2009 09:32:26 UTC</pubDate>
      <guid>https://snipplr.com/view/12077/pagination-madness</guid>
    </item>
  </channel>
</rss>
