I'm trying to understand and use cgraz's very nice example on pagination, copied here from another thread:
cgraz wrote:My these are popular. For all of you looking for Next Previous (pagination) Script, here it is
<?
/* Replace the following credentials with yours */
$server = "localhost";
$username = "user";
$password = "pass";
$database = "your_db";
$table = "your_table";
// Connect To MySQL Server
@mysql_connect($server, $username, $password) or die("Couldn't Connect to Database");
// Select Database
@mysql_select_db($database) or die("Couldn't Select Database");
// set number of results to display per page (in this case, 10 per page)
$pagelimit = "10";
// run query
$strSQL = mysql_query("SELECT * FROM $table") or die(mysql_error());
// count number of matches
$totalrows = mysql_num_rows($strSQL);
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($totalrows/$pagelimit);
// if no value for page, page = 1
if ($page==''){
$page='1';
}
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
echo "<b>" . $totalrows . " matches found</b><br>\n";
// Showing Results 1 to 10 (or if you're page limit were 15) 1 to 15, etc.
$starting_no = $start + 1;
if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 10 results per page and your script comes out with 12 results.
this will allow your script to say next 2 if you're on the first page and previous 10 if you're on the second page. */
if ($totalrows - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
$var2 = $totalrows - $end_count;
}
$space = " ";
// previous link (if you're on any page besides the first, create previous link)
if ($page>1) {
echo "« <a href='" . $PHP_SELF . "?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
// dynamic page number links
for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='" . $PHP_SELF . "?page=$i' class=main>$i</a>";
}
else {
echo " <b>[".$i."]</b>";
}
}
// next link (if the page you are on is less than the total amount of page numbers, there are more pages left)
if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='" . $PHP_SELF . "?page=".($page+1)."' class=main>Next " . $var2 . "</a> »";
}
/* output your data wherever you'd like.
BUT
in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT.
This will limit how many results are actually displayed on the page. */
$strSQL = mysql_query("SELECT * FROM $table LIMIT $start,$pagelimit") or die(mysql_error());
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output ten results per page.
change the $pagelimit variable to whatever to output more than 10 result per page. */
?>
There are plenty of ways to do this; this is just my way. It's all commented so make sure you understand how it works before copying and pasting.
Cgraz
My first question is you see how he calls
echo " <a href='" . $PHP_SELF . "?page=$i' class=main>$i</a>";
I understand that $PHP_SELF refers to the current php page. However, if the "current page" was called with parameters, as it ideally would be in this pagination script, does the $PHP_SELF include those parameters.
For example, my page that has the pagination script is a viewproducts.php. One example link to this page is as follows:
http://localhost/products/viewproducts.php?catID=aa&subcatID=fl
When I run the example code shown above, the $PHP_SELF is not including those parameters. If this is how $PHP_SELF works, in order to include the parameters, do i need to change those lines of code as follows:
echo " <a href='" . $PHP_SELF . "catID=$catID&subcatID=$subcatID&page=$i' class=main>$i</a>";
Thanks.