Get method for two variables - php

For some reason the paging does not work with the search get that I have although it was working fine in pages that don't use the get method. If I search for a term lets say example and I have eleven titles in the database that have in their title the word example 10 the first 10 should appear and then in the second page I should have the eleventh one. However when I try to go to the second page the search get from the first page is lost and I get ?pn=$i
<form class="sea" action="search.php" method="GET">
<input class="searchbar" name="search" type="text" placeholder="Search...">
<button class="searchbutton" name="submit-search" type="submit"><i class="fa fa-search"></i></button>
</form>
<?php
if(isset($_GET['submit-search'])) {
$search = mysqli_real_escape_string($conn, $_GET['search']);
$sql = "SELECT COUNT(*) FROM video_games WHERE video_games_title LIKE '%$search%' OR video_games_genre LIKE '$search'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$page_rows = 10;
$last = ceil($rows/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
}else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT * FROM video_games WHERE video_games_title LIKE '%$search%' OR video_games_genre LIKE '$search' ORDER BY video_games_id $limit ";
$query = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($query);
$paginationCtrls = '';
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<i class="fas fa-angle-left"></i> ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'"><i class="fas fa-angle-right"></i> ';
}
}
if ($queryResult > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["video_games_id"];
$title = $row["video_games_title"];
$genre = $row["video_games_genre"];
$image = $row["video_games_image"];
$description= $row["video_games_description"];
$platform= $row["video_games_platform"];
$price= $row["video_games_price"];
?>
//Putting the data from the database to the html
<?php }//end of while ?>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
If I do some replacements
$paginationCtrls .= ''.$i.' ';
When I press the second page I get the results from the first and the $paginationCtrls also say that I am in the first page eventhough in the url says search=example&submit-search=?pn=2
Why is it not working have I messed with the syntax somwhere?
What can I do to fix it?

Related

Pagination for category in php

I wrote a code pagination for category in php, this should display all items from database by selecting id and category id
It's displaying only first page, but when I click second page it's not displaying other list.
Where can I fix to display all items of list?
Here is my code:
<?php
include("conn.php");
$cid=intval($_GET['cid']);
$query=mysqli_query($conn,"select count(id) from adverts where category='$cid'");
$row = mysqli_fetch_row($query);
$rows = $row[0];
$page_rows = 3;
$last = ceil($rows/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
}
else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$query=mysqli_query($conn,"SELECT * FROM adverts WHERE category='$cid' $limit");
$paginationCtrls = '';
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
?>

Pagination only showing first 10 results instead of x amount of pages with 10 results on each

I have a piece of PHP code that is supposed to loop through all the results and make a new page for every 10 results, however it only shows the first 10 results and no options of next page or page numbers.
Is there a reason for this or have I missed something in the code I have written?
Thanks in advance.
My code is below;
$search_course = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$search_result = $result->fetch_assoc();
$row = mysqli_fetch_row($result);
//total rows for search
$rows = $row[0];
//number of results per page
$rows_per_page = 10;
//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
$last_page = 1;
}
$page_number = 1;
if(isset($_GET['pn'])){
$page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
//makes sure page number is between limits of $page_number
if($page_number < 1){
$page_number = 1;
} else if($page_number > $last_page){
$page_number = $last_page;
}
// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;
// query again only grabbing the set number of rows depending on page number
$search_course = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'
ORDER BY title DESC $limit";
$result = $mysqli->query($search_course) or die($mysqli->error);
$search_result = $result->fetch_assoc();
//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
//set up pagination
$pagination_controls = '';
if($last_page != 1){
if($page_number > 1){
$previous = $page_number - 1;
$pagination_controls .='previous ';
for($i = $page_number - 4; $i < $page_number; $i++)
{
if($i > 0){
$pagination_controls .= ''.$i.' ';
}
}
}
$pagination_controls.=''.$page_number.' &nbsm; ';
//clickable links to the left
for($i = $page_number+1; $i <= $last_page; $i++)
{
$pagination_controls .= ''.$i.' ';
if($i >= $page_number+4){
break;
}
}
if($page_number != $last){
$next = $page_number + 1;
$pagination_controls.=' Next';
}
}
$list = '';
while($row = mysqli_fetch_array($result)){
$title = $row['title'];
$id = $row['id'];
$list.='<p><a href="Selectedcourse.php">'.$title.' </p>';
}
mysqli_close($mysqli);
and the html element;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='courses.css'>
</head>
<body>
<div class="header">
<h1>Search Results for - <?= $_POST['searchBar'] ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php echo $search_result['summary']; ?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>
</body>
</html>
This is a good effort to roll your own pagination. It was mostly working. The problem was in your first query here:
$search_result = $result->fetch_assoc();
//This fetches only the first row of results
$row = mysqli_fetch_row($result);
//So this is not the total rows for search
$rows = $row[0];
Here's an updated script using prepared statements, which you really need to use. Unless you love being hacked.
//turn errors on to develop, back off when you go live
error_reporting(E_ALL);
ini_set('display_errors', 1);
//here are my main changes
$search_param = "%" . $_POST['searchBar'] . "%";
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ?");
$stmt->bind_param("s", $search_param); //learn this
$stmt->execute();
$result = $stmt->get_result();
//This gets the number of rows in a query result
$rows = $result->num_rows;
//number of results per page
$rows_per_page = 10;
//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
$last_page = 1;
}
if(isset($_GET['pn'])){
$page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
} else {
$page_number = 1;
}
//makes sure page number is between limits of $page_number
if($page_number < 1){
$page_number = 1;
} else if($page_number > $last_page){
$page_number = $last_page;
}
// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;
//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
//query again only grabbing the set number of rows depending on page number
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ? ".$limit);
$stmt->bind_param("s", $search_param);
$stmt->execute();
$result = $stmt->get_result();
$list = '';
while($row = $result->fetch_assoc()){
$title = $row['title'];
$id = $row['id'];
//I'm assuming you want each link to be different here...
$list.='<p>' . $title . '</p>';
}
mysqli_close($mysqli);
//set up pagination
$pagination_controls = '';
if($last_page != 1){
if($page_number > 1){
$previous = $page_number - 1;
$pagination_controls .='previous ';
for($i = $page_number - 4; $i < $page_number; $i++)
{
if($i > 0){
$pagination_controls .= ''.$i.' ';
}
}
}
$pagination_controls.=''.$page_number.' ';
//clickable links to the left
for($i = $page_number+1; $i <= $last_page; $i++)
{
$pagination_controls .= ''.$i.' ';
if($i >= $page_number+4){
break;
}
}
if($page_number != $last_page){
$next = $page_number + 1;
$pagination_controls.=' Next';
}
}
And finally the HTML section, with only one change:
<div class="header">
<h1>Search Results for - <?= $_POST['searchBar'] ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php /* echo $search_result['summary']; //Where was this coming from? */?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>

having trouble to integrate search results with pagination

I got the pagination controls and my search results shows 5 results fine. but when I click the second page, all of the search results go away. even though for what I searched, there is more than 5 results in the database. if you would please see the update code in just a second. here is my email bud just in case: raminrahim [at] hotmail [dot] com Please. thanks.
<?php
$paginationCTRLS = '';
$textline1 = '';
$textline2 = '';
$list = '';
if (isset($_GET['search_item'])) {
$search_item = $_GET['search_item'];
if (!empty($search_item)) {
if (strlen($search_item)>=3) {
// This query is just to get total count of rows
$query= "SELECT COUNT(item_id) FROM items"; // $sql
$query_run = mysqli_query($mysqli, $query); // $query
$query_row = mysqli_fetch_row($query_run); // $row
// $rows: here we have the total row count
$rows = $query_row[0]; // pgRows = row[]
// This is the number of results we want displayed per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure last cannot be less than 1.
if ($last < 1)
{
$last = 1;
}
// Establish the $pagenum variables
$pagenum = 1;
// Get pagenum from URL vars if it present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$query = "SELECT `item_id`, `featured_Items` FROM `items` WHERE
`featured_Items` LIKE
'%".mysql_real_escape_string($search_item)."%' $limit";
$query_run = mysqli_query($mysqli, $query);
$query_num_rows = mysqli_num_rows($query_run);
if ($query_num_rows>=1)
{
echo $query_num_rows. ' results found:<br>' . '<br>';
while ($query_row = mysqli_fetch_assoc($query_run))
{
echo $query_row['featured_Items'].'<br>' . '<hr>';
}
}
else
{
echo 'No result found' . '<hr>';
}
} else {
echo 'Not enough keywords to predict your search' . '<hr>';
}
}
$textline1 = "Result:" . '(<b>$rows</br>)';
$textline2 = "Page <b>" . '$pagenum' . "</b> of <b>" . '$last' . "</b>";
if ($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCTRLS .= 'Back ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i>0){
$paginationCTRLS .= ''.$i.' ';
}
}
}
$paginationCTRLS .= ''.$pagenum.' ';
for ($i = $pagenum+1; $i <= $last; $i++) {
$paginationCTRLS .= ''.$i.' ';
if($i >= $pagenum+4) {
break;
}
}
if ($pagenum != $last) {
$next = $pagenum +1;
$paginationCTRLS .= ' Next ';
}
}
$list = '';
while($row = mysqli_fetch_array($query_run, MYSQLI_ASSOC)) {
$id = $row["item_id"];
$availablesITEMS = $row["featured_Items"];
$list .= '<p>'.$availablesITEMS.'<br>.</p>';
}
}
echo $list . '<br/>';
echo $paginationCTRLS . '<br/>';
?>
You need to set up your query to be able to accept an start point.
Like this:
LIMIT 0, 10
Where the first number corresponds to the starting row your results are going to be obtained from, and the second number equals the amount of results you're going to get from that point on.
So in the sense of your code you'd have to set it up as:
$query = "SELECT `item_id`, `featured_Items` FROM `items` WHERE
`featured_Items` LIKE
'%".mysql_real_escape_string($search_item)."%'
LIMIT ".($pagenum-1)*5.",5";
Let me know if that clears up your doubt.
Edit:
As for enabling pagination through your means, simply change the method in your form to be a GET statement.
<form action="your_page.php" method="get">
Then from your code you can use the
$_GET['search_item']
method instead of POST. This in turn enables you to use those variables on your anchor tags for your pagination.
$paginationCTRLS .= ''.$i.' ';`
Edit2:
You have this piece of code:
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i>0){
$paginationCTRLS .= ''.$i.' ';
}
}
This will always at any point output the same number of pages, which is 5.
Now what you need is something that outputs only the pagination that you really need. So for example setting up this variable:
$pages = ceil($row/$page_rows);
Gives you your total amount of pages, then you can iterate to it:
for($i = 1; $i <= $pages; $i++){
if($i>0){
$paginationCTRLS .= ''.$i.' ';
}
}

After pagination only one row is displayed using PDO

I am in the process of changing over from mysql to PDO. How ever I am running into a few problems when I am trying to add page numbers to display the query results.
Here is the code:
$query_RS_Search = "SELECT * FROM products WHERE Category LIKE :category AND hidden = 'no'";
if (($_GET['Category']) =='Keyboards'){
switch( $_GET['price'] ){
case '':
$query_RS_Search .= ' AND Category != "Recent Keyboards" ORDER BY price ASC';
break;
case '0-500':
$query_RS_Search .= ' AND Category != "Recent Keyboards" AND products.price BETWEEN 0 AND 500 ORDER BY Keysound_price ASC';
break;
case '500-1000':
$query_RS_Search .= ' AND Category != "Recent Keyboards" AND products.price BETWEEN 500 AND 1000 ORDER BY price ASC';
break;
}
}else{switch( $_GET['price'] ){
case '':
$query_RS_Search .= ' ORDER BY price ASC';
break;
case '0-500':
$query_RS_Search .= ' AND products.price BETWEEN 0 AND 500 ORDER BY price ASC';
break;
case '500-1000':
$query_RS_Search .= ' AND products.price BETWEEN 500 AND 1000 ORDER BY Keysound_price ASC';
break;
}
}
$RS_Search = $conn->prepare($query_RS_Search) or die(mysql_error());
$RS_Search->bindValue(':category', '%' . str_replace('-', ' ',$_GET['Category']) . '%' );
$RS_Search->execute();
$row_RS_Search = $RS_Search->fetch();
$row = $RS_Search->rowCount();
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 7;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$RS_Search = $conn->prepare($query_RS_Search.$limit) or die(mysql_error());
$RS_Search->bindValue(':category', '%' . str_replace('-', ' ',$_GET['Category']) . '%' );
$RS_Search->execute();
// This shows the user what page they are on, and the total number of pages
$textline1 = "Testimonials (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
This only results one row when trying to display as follows:
<?php do { ?>
<?php echo $row_RS_Search['Manufacturer']; ?> <?php echo $row_RS_Search['Model']; ?> in <?php echo $row_RS_Search['Color']; ?>
<?php } while ($row_RS_Search = $RS_Search->fetch()); ?>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
I would like to display 7 rows per page.
Any help welcome
There is missing space in your $limit string which you concatenate later to prepare() statement.
// This sets the range of rows to query for the chosen $pagenum
$limit = ' LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

php mysqli prepared statement search result pagination, link to next page show empty

solved ..basically as hinted by YourCommonSense, using the get variable to pass through the search query so i added these chunck of code in on top and edited the linkable url to passthrough the variable.
if (isset($_GET['searchquery'])){
$searchquery=$_GET['searchquery'];
$stmt = $myConnection->prepare('SELECT COUNT(id) FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
$param = "%$searchquery%";
$stmt->bind_param('sssss', $param, $param, $param, $param, $param);
$stmt->execute();
/* store result */
$result=$stmt->bind_result($id);
$rows=array();
while ($stmt->fetch()){
$rows[]=array(
'id' => $id
);
}
$rows=$id;
// This is the number of results we want displayed per page
$page_rows = 4;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$stmt=$myConnection->prepare('SELECT id,product_name,price FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ? LIMIT ?,?');
$begin= ($pagenum - 1) * $page_rows;
$end= $page_rows;
$stmt->bind_param('sssssii', $param, $param, $param, $param, $param,$begin,$end);
$stmt->execute();
/* store result */
$stmt->store_result();
/* get the row count */
$count = $stmt->num_rows;
if ($count >= 1) {
$stmt->bind_result($id, $product_name, $price);
$search_output = "<hr />$count results for <strong>$searchquery</strong><hr />";
$textline1 = "<h4><center>Total of <b>$rows</b> items in this section</center></h4>";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
while ($stmt->fetch()) {
"$id, $product_name, $price";
$search_output .= "
<li><div class='product'>
<a href='product.php?id=$id' class='info'>
<span class='holder'>
<img src='inventory_images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
</div>
</li>
";
}
}
}
as mentioned in the question, i try to do my search result paginated. Everything else work, but the last little bit is when I click my paginatedCrls button go to next page or specific page, i loaded and show as a fresh new search. how do i fix this?
<?php
session_start();
$search_output = "";
if (empty($searchquery))
{ $search_output = "<hr />Please enter a search term in the above search box <hr />";}
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
$searchquery = preg_replace('/[^a-zA-Z0-9_ %\[\]\/\.\(\)%&-]/s', '', $_POST['searchquery']);
if ($_POST['filter1'] == "Products") {
require_once ("storescripts/connect_to_mysqli.php");
$stmt = $myConnection->prepare('SELECT COUNT(id) FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ?');
$param = "%$searchquery%";
$stmt->bind_param('sssss', $param, $param, $param, $param, $param);
$stmt->execute();
/* store result */
$result=$stmt->bind_result($id);
$rows=array();
while ($stmt->fetch()){
$rows[]=array(
'id' => $id
);
}
$rows=$id;
// This is the number of results we want displayed per page
$page_rows = 4;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$stmt=$myConnection->prepare('SELECT id,product_name,price FROM products WHERE product_name LIKE ? OR details LIKE ? OR category LIKE ? OR subcategory LIKE ? OR price LIKE ? LIMIT ?,?');
$begin= ($pagenum - 1) * $page_rows;
$end= $page_rows;
$stmt->bind_param('sssssii', $param, $param, $param, $param, $param,$begin,$end);
$stmt->execute();
/* store result */
$stmt->store_result();
/* get the row count */
$count = $stmt->num_rows;
if ($count >= 1) {
$stmt->bind_result($id, $product_name, $price);
$search_output = "<hr />$count results for <strong>$searchquery</strong><hr />";
$textline1 = "<h4><center>Total of <b>$rows</b> items in this section</center></h4>";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
while ($stmt->fetch()) {
"$id, $product_name, $price";
$search_output .= "
<li><div class='product'>
<a href='product.php?id=$id' class='info'>
<span class='holder'>
<img src='inventory_images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='product.php?id=$id' class='buy-btn'>RM<span class='price'>$price</span></a>
</div>
</li>
";
}
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}
?>
pass the 'searchquery' variable through from the url and get it back from the script
Learn MVC
You only pass the pagenumber to you pagination controlls, not the searchquery
Dont use $_SERVER['PHP_SELF']
Use proper escaping methods or validators instead of those preg_replaces to filter input

Categories