+ 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.
3 Réponses
+ 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
+ 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>
0
Thanks it worked but how can I create the "next and previous" link for the pagination?