+ 3

How can I create pagination from a MySQL database?

I want to create a online examination system using PHP. I created a database contains questions and options for each questions. But I have no idea about how to create pagination for each questions. Any help is appreciable.

16th Mar 2018, 6:54 PM
Arun K Soman
Arun K Soman - avatar
3 odpowiedzi
+ 7
In sololearn discussion you see "page" (?page=2) parameter in the url. And there's 20 questions in one page. You would get the page number with $_GET["page"]; and then use it in your mysql query like this. $count = 20; // Rows per page $page = $_GET["page"]; $to= 0; // Starting index if($page >= 2) { $to = $count * ($page - 1); } $query = $db->prepare("SELECT FROM table LIMIT $count OFFSET $to"); $query->execute(); $rows = $query->fetch(); So on ?page=2, $to variable will be 20. LIMIT 20 OFFSET 20 Selects 20 rows starting from 21
16th Mar 2018, 7:16 PM
Toni Isotalo
Toni Isotalo - avatar
+ 4
well, when you have the current page. <a href="?page=<?php echo $_GET["page"] - 1 ?>">Previous</a> <a href="?page=<?php echo $_GET["page"] + 1?>">Next</a>
6th Jul 2019, 4:45 PM
Toni Isotalo
Toni Isotalo - avatar
0
Thanks it worked but how can I create the "next and previous" link for the pagination?
16th Jul 2018, 7:36 PM
Li2Ekodaf
Li2Ekodaf - avatar