Jump to content


Photo

correctly using pagination math? (yeah, I suck at math... this should be simple)


  • Please log in to reply
1 reply to this topic

#1 MM23

MM23
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 11 October 2006 - 04:05 AM

OK, so I am working on an imageboard that sorts entries by tags. I have everything working but one thing: pagination. I want to be able to sort the entries when they're called to view by pages showing only twenty entries per page. I have it mostly working, but I think I might have to show you guys some of my code for it to make sense...

basically, if no page number is called through the url string, I have it set automatically to one, like so:

if (!isset($_GET['page'])) {
$current_page = 1;
}

There's where the simplicity ends, unfortunately, so I'll try to put as much detail into my mess of code below as I can:

I do a query to find the total number of rows in the table so far.
$query = "SELECT * FROM posts";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

I want to show 20 entries per page, so I set it up to do so. By my math I thought I could get the two limits for the sql query by multiplying the current page by 10 and 20. Therefore, if someone is on page 2, the two limits for the query would be 20 and 40, so you could give a mysql query to select * from table limit 20,40 or if it's page 4 select * from table limit 80,100. But I ran into my first problem when I realized that if they're on page 1, it will select everything from 10 to 20! I didn't want this, so I set it up to change the limits to 0 and 20 accordingly so it grabs the first 20 posts if it's on the first page  (very bad fix, I know, that's why I'm coming here to figure out a better way to do this math... yeah, I realize this place isn't elementary school, but I just can't figure out how to make this work :/)
$page_start = $current_page * 10;
$page_finish = $current_page * 20;
if ($num_rows <= 20) {
$total_pages = 1;
}

Naturally you would get the total number of pages by dividing the total number of rows in the table by 20, right? Since I want 20 per page.
else {
$total_pages = round($num_rows / 20);
}
if ($current_page == 1) {
$page_start = 0;
$page_finish = 20;
}

And here's the magic query that makes it all work.
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT $page_start,$page_finish";
Then I process the query, no need to include that code because I'm pretty sure it's all right.

Well, here is my next problem: I don't know how to get a workable number without rounding (how am I supposed to have 11.015252523052350250350253 pages or something like that?, and if I round it just doesn't work on some numbers. For example, let's say I have 21 rows in my table. I want it to show 2 pages of entries--- 20 on one, 1 on the other. 21 divided by 20 is 1.05, which php rounds down to 1, so it ends up just showing the one page of data. So yeah, I'm stumped. Please, please help me. Go ahead, chide me on my terrible math, but I don't care, as long as I get this working...

#2 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 11 October 2006 - 07:11 AM

How about:

      $ImageSet = $num_rows / 20;
      $total_pages = round($ImageSet);
      
if ($ImageSet > $total_pages) ++$total_pages;
   }
   if ($current_page == 1) {
      $Limit_finish = 20*$current_page;
      $Limit_start = $page_finish-20;
   }





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users