Pagination for category in php - 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 ';
}
}
?>

Related

Get method for two variables

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?

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 pagination page zero issue

My pagination is showing a page zero (0) when you go to page 2. Not sure why. I don't want to show a page zero.
I'll try to only show the necessary code.
Here is my code:
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
if (isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = 0;
}
*** $left_rec = countRecords() - ($page * $rec_limit); ***
$total_records = countRecords(); // countRecords() should be self-explanatory
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
$start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
foreach ($_GET as $key => $value)
{
if($key != "page") $querystring .= "$key=$value&";
}
echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>";
if ($left_rec < $rec_limit)
{
$last = $page - 1;
echo #"<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
}
else if($page == 0)
{
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
I would really appreciate the help in removing page 0 from the application. Please disregard any typos or missing quotes. The code works with the exception of it showing page 0.
I added a picture of what the application showing page 0. It only shows page 0 when I go to page 2. After that, I no longer see page 0.
Please let me know what I have to do.
Thanks.
Some advice:
You really shouldn't suppress errors using #, instead you should be instantiating all of your variables and writing proper code.
Don't hard-code pagination into each page. Instead, wrap it in a reusable function.
Example:
// draws a menu for navigating multiple pages of content
function paginate($page, $display, $total) {
if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
$query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
} else {
$query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
}
} else {
$query = '?page=';
}
$pages = $total <= $display ? 1 : ceil($total / $display);
$self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
$first = 'first';
$prev = 'prev';
$next = 'next';
$last = 'last';
echo '<p>';
echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
echo '(page ' . $page . ' of ' . $pages . ')';
echo ($page < $pages) ? "| $next | $last" : '| next | last';
echo '</p>';
}
// output example
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$display = 100;
$start = $display * $page - $display;
$total = countRecords($start, $display);
paginate($page, $display, $total);
I agree with #mister martin, but if you use this code fundamentally, try to change
$start = ($page < $adjacents ? 1 : $page - $adjacents);
To:
$start = ($page < $adjacents ? $page : $page - $adjacents);
Edited: maybe problem in undefined $left_rec, check demo. You can change $s like $_GET['page'] and as I see all works correctly.
Demo.
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();
// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);
echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>';
if($left_rec < $rec_limit && ($page > 1))
{
echo "<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
} else if ($page > 1)
{
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
} else {
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>

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