How to split search results into pages? - php

How to split the search results into pages? (like page 1, page 2, page 3...)
When the user searches for products on my e-commerce website, I want results to be split into several pages showing around 20 products per page. The search results are the outcome of database query.
For example: If the user searches for Samsung mobiles so my query will be:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';
Suppose the above query returns 55 results, how to show them into pages (1,2 and 3)?
I am using PHP, MySQL, Apache on Windows machine.

The appropriate SQL would be adding:
LIMIT start, amount
You can navigate like
search.php?start=20
and then code like:
LIMIT $start, $amount
with
$start = intval($_GET['start']);
and
$amount = 20;
That will result in max 20 records a page.

Use SQL's LIMIT keyword to limit the amount of results from your query; for example:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG' LIMIT 20, 40;
This would select 20 elements, starting at the 40th

Here is the complete code:
<?php
// Requested page
$requested_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Get the product count
$r = mysql_query("SELECT COUNT(*) FROM PRODUCTS WHERE BRAND='SAMSUNG'");
$d = mysql_fetch_row($r);
$product_count = $d[0];
$products_per_page = 20;
// 55 products => $page_count = 3
$page_count = ceil($product_count / $products_per_page);
// You can check if $requested_page is > to $page_count OR < 1,
// and redirect to the page one.
$first_product_shown = ($requested_page - 1) * $products_per_page;
// Ok, we write the page links
echo '<p>';
for($i=1; $i<=$page_count; $i++) {
if($i == $requested_page) {
echo $i;
} else {
echo ''.$i.' ';
}
}
echo '</p>';
// Then we retrieve the data for this requested page
$r = mysql_query("SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG' LIMIT $first_product_shown, $products_per_page");
while($d = mysql_fetch_assoc($r)) {
var_dump($d);
}
?>
Hope its help.

Yes you can run a query to get total record count and than use query using limit
exampe:
select count(id) from table_name
This will return total record count in database

In my php learning books, it provides a solution using a PHP class
it looks like this
<!-- language: php -->
<?php
error_reporting(0); // disable the annoying error report
class page_class
{
// Properties
var $current_page;
var $amount_of_data;
var $page_total;
var $row_per_page;
// Constructor
function page_class($rows_per_page)
{
$this->row_per_page = $rows_per_page;
$this->current_page = $_GET['page'];
if (empty($this->current_page))
$this->current_page = 1;
}
function specify_row_counts($amount)
{
$this->amount_of_data = $amount;
$this->page_total=
ceil($amount / $this->row_per_page);
}
function get_starting_record()
{
$starting_record = ($this->current_page - 1) *
$this->row_per_page;
return $starting_record;
}
function show_pages_link()
{
if ($this->page_total > 1)
{
print("<center><div class=\"notice\"><span class=\"note\">Halaman: ");
for ($hal = 1; $hal <= $this->page_total; $hal++)
{
if ($hal == $this->current_page)
echo "$hal | ";
else
{
$script_name = $_SERVER['PHP_SELF'];
echo "$hal |\n";
}
}
}
}
}
?>
then we call it on the script that require paging
<!-- language: php -->
<?php $per_page = 5;
$page = new Page_class($per_page);
error_reporting(0); // disable the annoying error report
$sql="SELECT * FROM table WHERE condition GROUP BY group";
$result=mysql_query($sql) or die('error'.mysql_error());
// paging start
$row_counts = mysql_num_rows($result);
$page->specify_row_counts($row_counts);
$starting_record = $page->get_starting_record();
$sql="SELECT * FROM table WHERE condition GROUP BY group LIMIT $starting_record, $per_page";
$result=mysql_query($sql) or die('error'.mysql_error());
$number = $starting_record; //numbering
$num_rows = mysql_num_rows($result);
if ($num_rows == 0 )
{ // if no result is found
echo "<div class=\"notice\">
<center><span class=note>NO DATA</span></center>
</div>";
}
else {
// while goes here ...
}
?>
// call the page link
<?php
$page->show_pages_link();
?>
hope it helps, just tried it hours ago to my search script page (learning from books)

Related

php pagination : page 2 shows same page 1 rows if new data added

i'm trying to make my own pagination system ignores the new data if someone trying to open the next page:
example:
i have this table :
1. apple
2. orange
3. banana
4. burger
5. pizza
6. spaghetti
and i only show 3 rows for each page.
mysql query:
// $page = 0 for first page
$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $page, 3";
the result is like this
page #1 :
6. spaghetti
5. pizza
4. burger
page #2 :
3. banana
2. orange
1. apple
let's say right now I'm in page #1 and someone else added 3 new rows:
water
juice
coffee
what will happen now is if i go to page #2 i will get a repeated page !
now page #2 is gonna be :
6. spaghetti
5. pizza
4. burger
the reason of that is because page #1 is showing the new data:
9. water
8. juice
7. coffee
how can i stop this to happen if the user didn't refresh the first page and only wanted to view the second page ?
it's a problem especially if you're trying to make your pagination looks just like twitter!
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM mytable";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
You can use something like this to get proper pagination for your table items. You can remove ORDER BY & DESC to know how the sql query result changes.
$page; // $page is current page
$result = 3; // As you need 3 results per page.
$offset = ($page * $result) - $result;
$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $result OFFSET $offset";
i had to use session to make it work as i want it , and here's how i solved it:
$count = $PDO->query("SELECT id FROM mytable")->rowCount(); //count rows
if(!isset($_SESSION['rowCount']))
{
$_SESSION['rowCount'] = $count; // save row counts
}
if(isset($_GET['page']))
{
$offset = ($_GET['page'] -1) * 3 + $count - $_SESSION['rowCount'];
}
else
$offset = 0;
$sql = $PDO->prepare("SELECT * FROM mytable ORDER BY id DESC LIMIT $offset, 3");
$sql->execute();
also i unset the session when user comes back to page 1.

Preventing bad input from GET variable

I have a variable, that gets the value from the URL.
if (isset($_GET['page'])) {
$getPage = $_GET['page'];
} else {
$getPage = "";
}
if ($getPage == "" || $getPage == "1") {
$page = 0;
} else {
$page = ($getPage * 6) - 6;
}
After it get this value it sends a query to the database, to ask for information. How can I make sure that the input is numeric and doesn't exceed the available amount?
Here is the query:
$query = "SELECT * FROM dbname LIMIT $page,6 ";
$select_all_list_items = mysqli_query($connection, $query);
Right now if i alter the url manually and put in a number that exceeds the page count, it shows nothing, or if I put in letters there it shows an error. In both cases I would like to redirect the user back to the first page.
To check numeric input and invalid page number,
$pageCount = $totalRecords / $recordsPerPage;
/* where $totalRecords is count of total records from db and $recordsPerPage is total rows per page */
if(!is_numeric($_GET['page']) || $_GET['page']>$pageCount){
$getPage = "1";
}
First you'll need to retrieve the total pages from your database:
$countQuery = 'SELECT COUNT(*) FROM dbname';
$countResult = mysqli_query($connection, $countQuery);
$countResultRow = mysqli_fetch_row($countResult);
$numPages = intval($countResultRow[0]);
Then you will need to implement some checks to your get variable:
if ($numPages < 0) {
throw new Exception('No pages to show!');
}
if (!is_numeric($getPage)) {
$getPage = 1;
}
if ($getPage > $numPages) {
$getPage = 1;
}
if ($getPage < 1) {
$getPage = 1;
}
Be careful passing GET values directly into your SQL query, this is a security risk as it can lead to database manipulation via URL. Read up about SQL injection for more information about "escaping" your data pre-query.
Something, like this
$page = 1; //default page 1
if(isset($_GET['page']) && preg_match('/[a-z]/i', $_GET['page'])){
// if set and alpha
header('Location: url of first page');
exit;
}else{
//not sure of this part but ( that's what the OP has so i'll go with it )
$page = ( $_GET['page'] * 6 ) - 6; ///could be 0 on page 1 6*1-6, just saying
}
Personally I'd run a query first to count the total rows, divide that by rows per page, for the total pages, and use that in the if statement... etc.

Pagination - Counting from 0 without mysql_result (shows error)

I am working on paginating my MySQL results to 10 per page. So far, I have been able the count how many results there are in the column, count how many pages there will be if there are 10 results/page, display the number of pages and also add links to each page (ex: Page1, Page2, Page 3, Page 4...)
Currently, all 38 results are showing on one page. When I click the page number, I am taken to the correct link but the content with the 38 results is the same. I know I need to divide the 38 results into the four pages. This is where I'm stuck at:
To find the number of pages, I have variable:
$pages = ceil($items_number / $per_page )
However, on multiple tutorials I've seen:
$pages = ceil(mysql_results($items_number,0) / $per_page )
which allows the data count to start at 0. When I tried this, I get an error
Fatal error: Call to undefined function mysql_results()
So I don't have the option to start counting results from 0 (using mysql_result) and break them into 10 per page.
How can I get around the mysql_result and be able to show the appropriate data/number of items for each page number?
Here's a link, clickable page numbers are on top:
http://test.ishabagha.com/classic_cars/pag_test.php
Code:
<?php
require_once("./includes/database_connection.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$page = "";
$per_page = 10;
$query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice FROM products WHERE `productLine` = 'Classic Cars'";
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
$query_count = "SELECT count(productLine) FROM products WHERE productLine = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count)
or die(mysqli_error($dbc));
$row = mysqli_fetch_row($items);
$items_number = $row[0];
$pages = ceil($items_number / $per_page )
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Home</title>
<link type="text/css" rel="stylesheet" href="classic_cars.css" />
</head>
<body>
<?php
require_once("./includes/navigation.php");
?>
<?php
for($number = 1; $number <= $pages; $number++) {
echo "<a href='?page=$number'>$number</a>";
}
while ($row = mysqli_fetch_array($result)) {
$product_code = $row['productCode'];
$product_name = $row['productName'];
$product_line = $row['productLine'];
$product_vendor = $row['productVendor'];
$product_description = $row['productDescription'];
$buy_price = $row['buyPrice'];
echo "<tr>
<td><p>$product_name</p></td>
</tr>";
} // end while ($row = mysqli_fetch_array($result))
?>
<?php
require_once("./includes/footer.php");
?>
</body>
</html>
Note:
It means that you are mixing deprecated mysql_* API with mysqli_*. You should use only one API in your project, and I would recommend to use mysqli_* only.
You forgot ; in your ceil($items_number / $per_page )
All the results are showing because you did not set a LIMIT on your query inside $query
In order to do this, you should know what page you are currently in your pagination
Get first the number of total rows (replace your previous one):
$items_number = mysqli_num_rows($items);
Let us determine what page you are currently, using $_GET["page"]. If no page is found in your URL, the default page to show is the first one.
Put this after your $pages = ceil($items_number / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page'])) { /* IF PAGE NUMBER IS VALID */
$currentpage = (int) $_GET['page']; /* STORE THE CURRENT PAGE */
} else {
$currentpage = 1; /* DEFAULT PAGE IS SET TO 1 */
}
if ($currentpage > $pages) { /* IF CURRENT PAGE IS 2 OR MORE */
$currentpage = $pages;
}
if ($currentpage < 1) { /* IF CURRENT PAGE IS LESS THAN 1, WHICH WE SHOULD PREVENT */
$currentpage = 1; /* DEFAULT PAGE IS SET TO 1 */
}
$offset = ($currentpage - 1) * $per_page; /* SET AN OFFSET FOR YOUR QUERY LATER */
Your query should look like this instead:
$query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice
FROM products
WHERE `productLine` = 'Classic Cars'
LIMIT $offset, $per_page"; /* LIMITS THE NUMBER TO SHOW IN A PAGE */
But we should move this query, and also the execution after the first code I had given above. So the arrangement is:
$query_count = "SELECT productLine FROM products WHERE productLine = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count) /* EXECUTE THE QUERY */
or die(mysqli_error($dbc));
$items_number = mysqli_num_rows($items); /* GET THE NUMBER OF RESULTS */
$items_number = number_format($items_number); /* SECURE THE FORMAT THAT IT IS A NUMBER */
$per_page = 10; /* NUMBER OF ROWS TO SHOW PER PAGE */
$pages = ceil($items_number / $per_page );
/* THE CODE I HAD GIVEN THAT GETS THE CURRENT PAGE AND DEFINES THE $offset */
/* THEN THE QUERY THAT LIMITS THE NUMBER OF ROWS TO FETCH */
/* THEN YOU FETCH THE RESULT, AND THEN THE HTML */

PHP paging database information

I am displaying search results from a database in my website. I am able to have the links to the number of pages show and have the first few results on the first page. But when I go to page 2 there is no results same with the 3rd and 4 pages that display.
php code
//This checks to see if there is a page number. If not, it will set it to page 1
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 5;
///////////set search variables
$property = $_POST['property'];
$bedroom = $_POST['BedroomNumber'];
$bathroom = $_POST['BathroomNumber'];
$priceMin = $_POST['PriceMin'];
$priceMax = $_POST['PriceMax'];
$sizeMin = $_POST['SizeMin'];
$sizeMax = $_POST['SizeMax'];
$termlease = $_POST['TermLease'];
//////////search
if(isset($_POST['utilities']) && is_array($_POST['utilities'])) {
foreach($_POST['utilities'] as $check) {
//echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
$sql = $mysqli->query("select * from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%' ORDER BY Price ASC LIMIT $start_from, 5");
if($sql->num_rows){
while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
echo '<div id="listing">
<div id="propertyImage">
<img src="uploadimages/'.$row['imageName1'].'" width="200" height="150" alt=""/>
</div>
<div id="basicInfo">
<h2>$'.$row['Price'].' | '.$row['Footage'].' sqft.</h2>
<p style="font-size: 18px;"># '.$row['StreetAddress'].', '.$row['City'].', BC</p>
<p>'.$row['NumBed'].' Bedrooms | '.$row['NumBath'].' Bathrooms | '.$row['Property'].'</p>
<br>
<p>View Full Details
</div>
</div>';
}
}
else
{
echo '<h2>0 Search Results</h2>';
}
$sqlPage = $mysqli->query("select count(id) from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'");
$row2 = $sqlPage->fetch_array();
$total_records = $row2[0];
$total_pages = $total_records > 0 ? ceil($total_records / 5) : 0;
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search.php?page=".$i."'>".$i."</a> ";
};
Contrary to your overview's select you've got no filter on the select used for the page-count. As such the latter will be (potentially) higher, thus indicating a page that isn't there. You should change the $sqlPage line to the following to get a matching amount of pages.
$sqlPage = $mysqli->query("select count(id) from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'");
Also you should change the $total_pages line to the following, as you might otherwise get a division by 0 error:
$total_pages = $total_records > 0 ? ceil($total_records / 5) : 0;
EDIT irt comments below
If you go to the next page data doesn't get reposted and thus al you parameters are empty. There are a couple of ways to ensure the data is available on the next pages. You could output a form and repost it, you could append the data to the GET query, or you can store it in a session, a file or a database. Anyway, a quick solution in your case would be adding the following code just before the set search parameters part, which saves POST daat to a session and then uses that data untill new data is posted:
if(!isset($_SESSION)) {
if(!#session_start()) die('Could not start session');
}
if(empty($_POST)) {
if(isset($_SESSION['searchpost'])) $_POST = $_SESSION['searchpost'];
} else {
$_SESSION['searchpost'] = $_POST;
}

Pagenav. By clicking next get next lets say "20" records (php/mysql)

I've already created a query to mysql that will give 20 results from mysql table etc. "cat"
heres the calling:
if(isset($_GET['cat']))
{
$query = "SELECT game_title,game_desc,....
FROM games WHERE cat_id='".validate_input($_GET['cat'])."' LIMIT 20";
}
...
by this I manage to get the results I wanted. What I am asking here is how can I create a "button" that will load the next "20" records from table "cat" (something like Buttons).
<?
$cn=mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("db56") or die(mysql_error());
$sql="select count(*) from emp";
$result=mysql_query($sql);
$r=mysql_fetch_row($result);
$record=$r[0];
$pagesize=20;
$totalpages=$record/$pagesize;
$currpage=$_GET["pg"];
if(!isset($currpage))
$start=0;
else {
$currpage--;
$start= $currpage * $pagesize;
}
$end=$start+$pagesize;
$sql="select * from emp limit $start,$pagesize";
$result=mysql_query($sql);
if($result){
print "<table border='1'>";
print "<tr><th>No</th><th>Name</th><th>Date</th></tr>";
while($r=mysql_fetch_row($result))
{
print "<tr><td>$r[0]</td><td>$r[1]</td><td>$r[2]</td></tr>";
}
print "</table>";
}
for($i=1;$i<=$totalpages;$i++){
print "<a href='listemp.php?pg=$i'> $i </a>";
}
?>
If you keep track of what page you're on, in the URL or a hidden field or session variable, you can use that to work out the limit. For example, page 2 should show limit 20 from row 20 (20 times the page offset).
You can pass two SQL parameters to LIMIT (put a comma between them) If you do, the first tells SQL how many records to skip (ie the offset of the first record) and the 2nd parameter is the one you're already using (how many records to return).
So just put a variable in your "next" link that says what page to display. You can pass the offset in this link, but it's pretty common to pass the "page number" instead, and multiply by the number of records per page before sticking it in the sql.
next page
With a bit more work, you can make a "previous page" link, and make the correct links non-clickable when you're at the first/last page.
Pass along a parameter that tells the script that you want another chunk of the results and not just the first batch.
So for the link it could be like this:
example.com/results.php?cat=1&page=2
Where page= will tell the script what page you want to return.
Then you want to turn that LIMIT number you have so that you can work out some simple maths
$results_cnt = 20; //--rows you want per page of results
Now in your script you'll check to see if the page variable has been set. If not, default the start row to return from the first. But as you want to return different pages/sets of results, a little math is needed in order to start at the proper row.
if(isset($_GET["page"]) //--see if the variable is even there
{
$page_num = (int)$_GET["page"]; //--forcing it to always be an integer
$start_row = $results_cnt * ($page_num - 1);
/* --
what happens:
($results_cnt currently at 20)
on page one (page=1), start at row 0
math: 20 * (1 - 1) = 0
on page two (page=2), start at row 20
math: 20 * (2 - 1) = 20
on page three (page=3), start at row 40
math: 20 * (3 - 1) = 40
etc.
*/
}
else
$start_row = 0;
Now, having set the correct starting row, adjust the SQL query to use the variables like so:
if(isset($_GET['cat']))
{
$query = "SELECT game_title,game_desc,....
FROM games
WHERE cat_id='".validate_input($_GET['cat'])."'
LIMIT $start_row, $results_cnt";
}
OK I got it fixed I guess...
in index.php:
<?php
$games = array();
$count = 1;
$total = 0;
if(isset($_GET['cat']))
{
$query = "SELECT game_title,game_desc,.... FROM games WHERE cat_id='".validate_input($_GET['cat'])."' LIMIT 20";
$total = mysql_num_rows(mysql_query("SELECT 1 FROM games WHERE cat_id='".validate_input($_GET['cat'])."'"));
}
$query_result = #mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
while ($info = #mysql_fetch_array($query_result))
{
/* -- here goes the infos.. */
$games[$info['game_title']]['game_title'] = $info['game_title'];
etc..
if(isset($_GET['cat']))
{
/* -- for template engine */
$page->SetLoop ('PAGES', pagenav($total,$_GET['page'],20,$config['site_url'].'?cat='.$_GET['cat'],1,$lang));
}
--end php
and in funct.php
--start php
function pagenav($total,$page,$perpage,$url,$posts=0)
{
$page_arr = array();
$arr_count = 0;
if($posts)
{
$symb='&';
}
else
{
$symb='?';
}
$total_pages = ceil($total/$perpage);
$llimit = 1;
$rlimit = $total_pages;
$window = 5;
$html = '';
if ($page<1 || !$page)
{
$page=1;
}
if(($page - floor($window/5)) <= 0)
{
$llimit = 1;
if($window > $total_pages)
{
$rlimit = $total_pages;
}
else
{
$rlimit = $window;
}
}
else
{
if(($page + floor($window/2)) > $total_pages)
{
if ($total_pages - $window < 0)
{
$llimit = 1;
}
else
{
$llimit = $total_pages - $window + 1;
}
$rlimit = $total_pages;
}
else
{
$llimit = $page - floor($window/2);
$rlimit = $page + floor($window/2);
}
}
if ($page>1)
{
$page_arr[$arr_count]['title'] = 'Prev';
$page_arr[$arr_count]['link'] = $url.$symb.'page='.($page-1);
$page_arr[$arr_count]['current'] = 0;
$arr_count++;
}
for ($x=$llimit;$x <= $rlimit;$x++)
{
if ($x <> $page)
{
$page_arr[$arr_count]['title'] = $x;
$page_arr[$arr_count]['link'] = $url.$symb.'page='.($x);
$page_arr[$arr_count]['current'] = 0;
}
else
{
$page_arr[$arr_count]['title'] = $x;
$page_arr[$arr_count]['link'] = $url.$symb.'page='.($x);
$page_arr[$arr_count]['current'] = 1;
}
$arr_count++;
}
if($page < $total_pages)
{
$page_arr[$arr_count]['title'] = 'Next';
$page_arr[$arr_count]['link'] = $url.$symb.'page='.($page+1);
$page_arr[$arr_count]['current'] = 0;
$arr_count++;
}
return $page_arr;
}
?>
and call it by
{LOOP: PAGES}{PAGES.title} {/LOOP: PAGES}
in an .html file..
it works perfectly but when I press the number 2 or next to get the next 20 records it jumps back to the first 20...
on the browser it reads http://siteurl/?cat=1&page=2
I can't figure out why.

Categories