See the URL you're constructing:
<a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">
This turns into, for example:
script.php?query=something&page=10&limit=10
So, your URL creates variables query, page and limit on each access. These variables are available as keys in the $_GET superglobal variable:
$GET['query'], $GET['page'], $_GET['limit']
Since from the beginning of your script you're not pulling these, but you use $page and $limit immediatelly, it suggests that your server setup is such that it creates (registers) the variables in URL as global PHP variables $query, $page and $limit.
So, if your server is NOT set up to register such globals, the $query, $page and $limit will be empty, unexisting variables and will technically, per your script, contain no values. Since you use those values to construct the database query, whatever you click it will always load same page.
For starters, at the beginning of your script, fetch the values from the $_GET superglobal:
$query= $GET['query'];
$page= (int) $GET['page'];
$limit= (int) $_GET['limit'];
<?
/* Customizable pagination with alternating table row colors and
clickable header ordering This is the result of my pains and the
application of other's wisdom.
Special thanks to Loafin for his code on table row colorization!
Feel free to modify and use at your discression.
If this helps you out, it would be nice to hear from you, so you can e-
mail me at chaosdivine@hotmail.com.
Peace!
Chaosdivine
*/
$db_addr = 'localhost'; // address of MySQL server.
$db_user = ''; // Username to access server.
$db_pass = ''; // Password access server.
$db_name = 'domaintrader'; // Name of database to connect to.
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");
if (!($connect)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}
if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $db_name database.</p>");
exit();
}
// If your server does not register globals, you need to pull query, page and limit from the URL
$query= $_GET['query'];
$page= (int) $_GET['page'];
$limit= (int) $_GET['limit'];
if (!($limit)){
$limit = 10; // Default results per-page.
}
if ( !$page or $page < 0 ) {
$page = 0; // Default page value.
}
/*
$numresults = mysql_query("SELECT * FROM domains"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
*/
// To fetch the count of rows, use this instead
$numresults= mysql_query('SELECT COUNT(*) FROM domains');
$numrows= mysql_result ($numresults, 0);
if ($numrows == 0){
echo("No results found matching your query"); // modify the "Not Found" error for your needs.
exit();
}
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows % $limit) {
$pages++; // has remainder so add one page
}
$current = intval($page/$limit) + 1; // Current page number.
if (($pages < 1)) {
$total = 1; // If $pages is less than one, total pages is 1.
}
else {
$total = $pages; // Else total pages is $pages value.
}
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit; //If not last results page, last result equals $page plus $limit.
}
else{
$last = $numrows; // If last results page, last result equals total number of results.
}
//escape from PHP mode.
?>
<html>
<head>
<title>Customizable Pagination With Alternating
Table Row Colors and Clickable Header Ordering</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="100%" border="0">
<tr>
<td colspan="2" align="left"><font face="Arial, Helvetica, sans-serif"><font
size="1">Results
per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a>
| <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a></font>
</font></td>
</tr>
<tr>
<td colspan="2" align="left"> </td>
</tr>
<tr>
<td width="50%" align="left" valign="top"><font
size="2" face="Arial, Helvetica, sans-serif">Showing
Results <b>
<?=$first?>
</b> - <b>
<?=$last?>
</b> of <b>
<?=$numrows?>
</b></font> </td>
<td width="50%" align="left" valign="top"><div align="left"><font
size="2" face="Arial, Helvetica, sans-serif">Page
<b>
<?=$current?>
</b> of <b>
<?=$total?>
</b></font></div></td>
</tr>
<tr>
<td height="21" colspan="2" align="left"> <font
face="Arial, Helvetica, sans-serif"> </font></td>
</tr>
<tr>
<td height="21" colspan="2" align="left">
<?
//$limitnum = 15;
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit&order=$order\"><< Back</a>\n");
}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");} // If current page don't give link, just text.
//elseif ($ppage < $page - $limitnum || $ppage > $page + $limitnum){}
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit&order=$order\">$i</a>\n");}
}
if (!((($page+$limit) / $limit) >= $pages) &&
$pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit&order=$order\">Next >></a>");
}
?>
</td>
</tr>
</table>
<?
//Go back into PHP mode.
$default_sort = 'domains'; //set default sort ordering scheme
$allowed_order = array ('domains', 'price'); //these are the allowable fields for ordering
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
$results = mysql_query("SELECT *
FROM domains
ORDER BY $order
LIMIT $page, $limit"); //defines the query parameters
// Now we can display results
//defines the clickable sort ordering by header title while maintaining $limit (listing offset)
?>
<TABLE width="50%" BORDER="0">
<TR bgcolor="#4f5c38">
<TD><font color="white"><a href="<?="$PHP_SELF?query=$query&limit=$limit&order=domains"?>">Domain Name</a></font></TD>
<TD><font color="white">Date Submitted</font></TD>
<TD><font color="white"><a href="<?="$PHP_SELF?query=$query&limit=$limit&order=price"?>">Asking Price</a></font></TD>
</TR>
<?php
//displays the results and starts the row colorization
for($i = 0; $i < $limit; $i++){
for ($count = 1; $row = mysql_fetch_assoc ($results);++$count) {
if ($count & 1) {
$color = "#c8cfc6";
$font = "yellow";
} else {
$color = "#9aa486";
$font = "white";
}
$printthis = "<tr>
<td bgcolor=\"$color\"><font color=\"$font\">$row[domains]</font></td>
<td bgcolor=\"$color\"><font color=\"$font\">$row[domainDate]</font></td>
<td bgcolor=\"$color\"><font color=\"$font\">$row[price]</font></td>
</tr>";
print $printthis;
}
echo "</TABLE>";
}
?>
</body>
</html>
If you have access to PHP.ini check the directive register_globals. If it is ON then you don't need to pull from $_GET since the vars in url will automatically become available as PHP vars. This, however, poses great security risk and I suggest you to set it, or leave it at, OFF.
If you don't have access to php.ini, make a PHP file in which you issue simple call to phpinfo(); This will publish all the relevant information about your PHP and Apache settings, where you can see if register_globals is on or off. Remove this file as soon as you don't need it, since you don't want potential hackers to see the setup of your server.
Also, the $query variable is never used in this script, so I don't know why you're using it since your url's are self-referencing. Be careful what you pass through that variable, though. If it is something you will insert into your SQL query, it can become great security risk and means of SQL injection.
Also see these:
http://www.php.net/manual/en/language.variables.predefined.php
http://www.php.net/register_globals