I’m used to working with PHP 5x and oldschool MySQL, so I’m stumped as to how to add pagination to this script. Any pointers would be welcome. I know I have to add a LIMIT clause to my query, but anything I’ve tried so far to create pagination hasn’t been working. Many of the examples I’ve found don’t work with PHP7/ PDO,
Thanks for your help.
//initialize the variable we're going to pass through the URL so it isn't empty
if(isset($_GET['search'])){
$search = "%".$_GET['search']."%";
}else{
//if there's no search variable in the url, that means you've just arrived on the page, so exit the script and display a welcome message with instructions
$search = " ";
echo '<h2>What are you looking for?</h2><p>Please try searching for something. </p>';
include_once($pageendpath);
exit;
}
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=$charset;port=$port", $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//set up the SQL statement as a variable
$sql= "SELECT * FROM databasenane WHERE text LIKE :text order by id DESC";
//prepare the SQL query stored in variable $sql, see how the placeholder exists above
$stmt = $conn->prepare($sql);
//bind the paramater in the placeholder
$stmt->bindParam(':text', $search, PDO::PARAM_STR);
//execute the query
$stmt->execute();
//display a row count
$total = $stmt->rowCount();
//echo back the text variable as a new variable, called textreflect, without the concatenated percentage symbols in the original variable
$textreflect = $_GET['search'];
echo 'Your search for <strong>"'. $textreflect. '"</strong> uncovered '.$total.' result(s).<br>';
// here's the while loop, for dealing with search results
while ($row = $stmt->fetchObject()) {
//start displaying the items retrieved
echo '
the code in here for displaying the results
';
}
//close the database connection once it's done returning results
$conn = null;