Zac Brown
Zac Brown

Reputation: 6063

Display Content on multiple pages using PHP

In one of my Facebook Apps, I want to add the ability for the user to view the gifts that were sent to them, even after they have accepted them. The problem is, the page would be entirley too long if they were all displayed on one. How would I go about displaying up to 8 images per page. The probelm is, I don't know exactly how the second, third, etc pages are created to display.

What I want is just like a search engine. Display so many, then go to the next page.

Upvotes: 0

Views: 1306

Answers (2)

Ahtsham Farooq
Ahtsham Farooq

Reputation: 122

Use a page identifier to detect page number. The page number should be 0,1,2,3..

Then multiply it with number of results per page to get starting row from where you want next 8 results.

For example you have a variable called pageNumber for current page .

A limit variable to get starting row.

    resultPerPage = 8; 
    limit =(pageNumber)*(resultPerPage);

Then the query will be

    SELECT * FROM gifts WHERE somevar = ? LIMIT limit , 8;

Now if your page number is 0 then limit will be 0 and if page number is 1 then limit will be 8 vice verse..

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

You have the gifts they were sent in some database, right?

SELECT from that table 8 rows at a time. Use a LIMIT clause to specify that you want 8 rows, and from which row to start picking those 8.

http://dev.mysql.com/doc/refman/5.1/en/select.html

The offset is the page number, minus 1, multiplied by 8.

Ex for page 3:

SELECT gift_name FROM gifts WHERE user = ? LIMIT 16,8

Upvotes: 2

Related Questions