Question

I'm building a webpage where the items listed in the webpage are dynamic, (The items are loaded from the database.) Say we have 19 items in the database. The maximum items allowed for a page is 4. Now I have 15 items remaining. I need the rest of the items in another 4 pages.

Like this --> Pg1 -> 4 items displayed
              Pg2 -> 4 items displayed
              Pg3 -> 4 items displayed
              Pg4 -> 3 items displayed

As shown above my php code needs to identify the number of items and the number of pages required. What is the best way you would suggest me to do this.

Was it helpful?

Solution

Get the count of items in the database.

select count(whatever_id) as numberofwhatevers from mytable;

save it (in a variable or a class or however you feel comfortable)

$recordcount = $results["numberofwhatevers"];
$recordsperpage = 4;
$numberofpages = ceil($recordcount / $recordsperpage);

The only complicated bit is querying a subset of the records to match whatever page you're on... for example, if you're on page 3, you want records 9-12.

select * from mytable limit 9,4;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top