-1

I wrote a PHP code that selects data from the database and displays them on the web page. But I'm stuck in writing the page pagination code. And also I want a Bootstrap page pagination.

<?php  

$sql = "SELECT * FROM `links` WHERE `title` LIKE '%$search_text%' OR `keywords` LIKE '%$search_text%' OR `description` LIKE '%$search_text%' ";
$result = $con->query($sql);
$rowcount=mysqli_num_rows($result);

if ($result->num_rows > 0) {

echo "<p class='m-0 pl-3 h5' style='font-size:1.45vw;'>Received informations about <strong>$rowcount</strong> websites!";
  // output data of each row
  while($row = $result->fetch_assoc()) {
    $title = $row["title"];
    $onion_address = $row["onion_address"];
    $description = $row["description"];

echo "
<div class='m-0 p-3'>
<h4 class='p-0 m-0' style='color:blue;font-size:2.2vw;'>$title</h4>
<p class='h6 text-success font-weight-bold' style='font-size:1.3vw;'>$onion_address</p>
<p class='h6' style='font-size:1.3vw;'>$description</p>
</div>
";

  }
} else {
  echo "0 results";
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Aug 02 '21 at 09:54

1 Answers1

0

Implement a LIMIT in your sql and select using an offset based on the LIMIT * PAGE NUMBER.

SELECT * FROM foo WHERE bar LIMIT page_limit OFFSET page_limit * page_number

This will allow you to select only the amount of entries needed for that particular page, as opposed to getting all of the data and splitting it into pages in your php

David McIntyre
  • 218
  • 1
  • 9