Counting up, but with Page Numbering....?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Counting up, but with Page Numbering....?

Post by simonmlewis »

Hi

Got a problem that seems so lame, but I cannot fathom how to fix it.

I have a page that displays people's names, and by each name it counts up by 1.

ie.
1 Fred
2 Joe
3 Sally.

But the page produces a few hundreds, so I have page numbering, and when you go to Page 2 (or over), it restarts at one.
This is the simple code I use for the 1, 2, 3....

Code: Select all

$start = $start + 1;
echo "$start";
How do I make each page (there are 7 at the moment) of 40 carry on from the previous $start number?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Counting up, but with Page Numbering....?

Post by pickle »

You want to research "pagination".

Generally this is accomplished with the LIMIT clause of the query.

ie: If you're on page 1, the query would look like:

Code: Select all

SELECT
  *
FROM
  `table`
LIMIT 0,40
If you're on page 2:

Code: Select all

SELECT
  *
FROM
  `table`
LIMIT 41,40
You generally store the page number in the URL: http://yourwebsite.com/results/?page=2
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Counting up, but with Page Numbering....?

Post by simonmlewis »

Sorry I don't think I made my self terribly clear then.
I don't need page numbering: that is sorted.
I need to count up one by one to show by each person.
Please reading my post again. I don't need page numbers.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Counting up, but with Page Numbering....?

Post by pickle »

Ah, I see.

You can still store the $start in the URL & initialize to 1 if it's not found.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Counting up, but with Page Numbering....?

Post by McInfo »

Basically, the math is

Code: Select all

first_line_number = (page_number - 1) * number_of_lines_per_page + 1
assuming that page numbers start at 1 and line numbers start at 1 on the first page.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Counting up, but with Page Numbering....?

Post by simonmlewis »

Bingo.

Code: Select all

    if(isset($_GET['start']))
      {
      $start = $_GET['start'];
      } 
Before the MySQL query.
Then the SELECT query.
Then after 'WHILE' {...

Code: Select all

 $start = $start + 1;
echo "$start"; 
Just took a bit of working out in which order and placement it had to go. But works great. Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply