Row Divider

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
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Row Divider

Post by ScottCFR »

I am working on bloging software, in which right now shows every post. I know there is some mathematical equasion to make it show like 20 per-page. I don't know it.

Summary:

Could someone make a code to only allow 20 posts to show per page. Also, an overflow generator that would create the second page, third page, fourth and so on. If possible also explain this for future reference.

Thanks in advance,
Scott W.
ginocyber
Forum Newbie
Posts: 4
Joined: Wed Sep 15, 2010 11:35 am

Re: Row Divider

Post by ginocyber »

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Row Divider

Post by alex.barylski »

Equation? There is no equation, it's called pagination. :)

Code: Select all

$sql = "SELECT * FROM table LIMIT, 25";
That SQL code will SELECT all records from 1 to 25, if you wanted to SELECT all records on second page, you would say, 26, 51. Perhaps you meant that equation, for calculating pagination details?

1. Determine the total number of records in your resultset, lets say 1000.
2. Determine how many records you want displayed per page, lets say 10.
3. Calculate the total number of pages needed to partition your resultset, 1000 / 10 = 10

You have 10 pages, each with 10 records each totaling 1000 records.

Cheers,
Alex
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Row Divider

Post by buckit »

you'll be hard up for someone here to write it FOR you. but we can definitly help.

what you are trying to do is called paginating. there are lots of threads on this forum and google that will help.

basically what you want to do is:
1) use an SQL count statement with your query to get how many results you are working with.
2) calculate how many pages there will be based on how many you want to display per page.
3) in your SQL SELECT statement you will want to use LIMIT command to limit your results to 20.

for example (with LIMIT): LIMIT 0, 20 would be your first SQL statement. the first variable is where to start (0 = the beginning) and the second is how many per page.

so in reality you would want have something like this: (SELECT * FROM table LIMIT {$offset}, {$per_page})

we use variable $offset because your offset is going to change... we also use $per_page because you may want to make that user selectable.

so your next button would send a value to change the offset. so for page 2: LIMIT 20, 20. for page 3: LIMIT 40, 20.

Make sense? I know I didnt go into mush detail... but like I said, there are lots of other threads on here and google that can help you figure it out the rest of the way.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Row Divider

Post by califdon »

PCSpectra wrote:1. Determine the total number of records in your resultset, lets say 1000.
2. Determine how many records you want displayed per page, lets say 10.
3. Calculate the total number of pages needed to partition your resultset, 1000 / 10 = 10

You have 10 pages, each with 10 records each totaling 1000 records.
@PCSpectra: You're a PHP genius, but you flunk Arithmetic! :rofl:
Post Reply