I've a problem with pagination of php mysql results. The user must be able to enter something in the text field, and when the 'send' button is pressed, it must immediately show the first ten results (and this now does it well). The problem is that the links (1, 2,3, etc.) don't work: in a certain way they return to an initial situation in which there is the pagination of all the data contained in my mysql database.
In my opinion the problem is with this line of my code: echo "<a href='prova.php?page=".$page."'>".$page."</a> -- ";
, where I'm getting the link wrong. I have already searched a lot on the Internet and especially here on stackoverflow.com for possible solutions to my problem, but I've not found anything exhaustive to solve my problem. Where am I wrong in your opinion? The file name is prova.php.
<!-- The code may contain words taken from the Italian language -->
<html lang="it-IT">
<head>
<link rel="icon" href="https://image.flaticon.com/icons/png/512/1427/1427106.png">
<title>Pagination tests</title>
</head>
<body style="background: #ECF0F1;font-family: Myanmar Text;margin-top:90px;">
<form action="prova.php?page=1" method="get"><input value="<?php echo $_GET['text']; ?>" type="text" name="text"><button name="send">INVIA</button><br></form>
<?php
include 'functions.php';
$con = mysqli_connect('localhost', 'root', '', 'cherubini');
$search = $_GET['text'];
if(isset($_GET['send'])) {
$search = $_GET['text'];
}
$result = mysqli_query($con, "SELECT *
FROM lemmi
WHERE LemmaNo LIKE '%".$search."%'");
$number_of_results = mysqli_num_rows($result);
$number_of_pages = ceil($number_of_results / 10);
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$this_page_first_result = ($page - 1) * 10;
$result = mysqli_query($con, "SELECT * FROM lemmi WHERE LemmaNo LIKE '%".$search."%' LIMIT ".$this_page_first_result.',10');
while ($row = mysqli_fetch_array($result)) {
echo "<b>". highlight($search, $row['Lemma']) . '</b> ' . $row['Definizione'] . "<br>";
}
for($page = 1; $page <= $number_of_pages; $page++) {
echo "<a href='prova.php?page=".$page."'>".$page."</a> -- ";
}
?>
</body>
</html>