and still i already made a class for this so that u dont need to cut & paste it in every page with the same functions
here it goes.
filename: testQuery.php
<?
include ("dbConnect.php");
if ($rstart == ''){$rstart=0;}
if ($pgptr == ''){$pgptr=1;}
$rowsPerPage = 10;
if($rstart > 10){ $toValue = $rstart + $rowsPerPage; }
include ("queryList.php");
$sql = "select * from users";
$makeQuery = new queryList( $sql, $rstart, $pgptr, $rowsPerPage, 10, 'testQuery.php' );
$res = mysql_query ($makeQuery->sqlQry);
# list elements in table with changing background on each row
?>
<table cellpadding=0 cellspacing=0 width=600>
<tr bgcolor=#808080 height=25>
<td width=100> <b>Country Code</b></td>
<td width=500><b>Country</b></td>
</tr>
<?
while ( list ( $code, $country ) = mysql_fetch_row($res) ){
if($bgcolor=='#C9C9C9'){ $bgcolor='#DFDFDF'; } else { $bgcolor='#C9C9C9'; }
echo "<tr bgcolor=$bgcolor height=25>";
echo "<td> $code</td>";
echo "<td>$country</td>";
echo "</tr>";
if($rstart <= 10){ $toValue++; }
if ($makeQuery->totalRows >= 1){ $message = "Displaying records <font color=red>$makeQuery->fRow</font> - <font color=red>$toValue</font> of <font color=red>$makeQuery->totalRows</font>."; }
}
?>
</table>
<?
echo "<br>".$message;
if($makeQuery->totalPages > 1){
echo "<br>Pages ( $makeQuery->totalPages ): ".$makeQuery->pages;
}
?>
filename: queryList.php
<?
/**
** Author: Oliver Susano (vher_98@yahoo.com)
** Class queryList
** Query with paginations and previous next links.
** syntax: queryList($sql, $rstart, $pgptr, $rowsPerPage, $pageLimit, $link)
**/
class queryList {
function queryList($sql, $rstart, $pgptr, $rowsPerPage, $pageLimit, $link) {
$result = mysql_query( $sql );
$totalRows = mysql_num_rows($result);
$this->totalRows = $totalRows;
$totalPages = intval($totalRows / $rowsPerPage);
$excess = $totalRows % $rowsPerPage;
if ($excess > 0){ $totalPages = $totalPages + 1; }
$trstart = $rstart;
$pages = "";
if($totalPages > $pageLimit ) { $value = $pageLimit; } else {$value = $totalPages;}
if($pgptr > $pageLimit){ $i = $pgptr - $pageLimit; $value = $pageLimit+$i;}
// section for Previous Record
if($pgptr > 1){
$ppgptr = $pgptr - 1;
$prstart = $rstart - $rowsPerPage;
$pages = $pages." <b>[ <a href='".$link."?pgptr=$ppgptr&rstart=$prstart'>prev</a> ] </b>";
}
while ($i < $value){
$trstart = $rowsPerPage * $i;
$i++;
if ($i == $pgptr){
$pages = $pages."<font size=2><b>$i</b></font> ";
} else { if($i <= $totalPages){ $pages = $pages."<a href='".$link."?pgptr=$i&rstart=$trstart'>$i</a> ";} }
}
// section for Next Record
if($i <= $totalPages){
if($totalPages != $pgptr){
$nrstart = $rstart+$rowsPerPage;
$npgptr = $pgptr + 1;
$pages = $pages." <b>[ <a href='".$link."?pgptr=$npgptr&rstart=$nrstart'>next</a> ]</b>";
}
}
$fRow = (($pgptr-1) * $rowsPerPage) + 1;
$i = $fRow - 1;
$this->pages = $pages;
$this->fRow = $fRow;
$this->totalPages = $totalPages;
$this->sqlQry = $sql." LIMIT ".$rstart.",".$rowsPerPage;
} // end of query()
} // end of Class
?>
filename: dbConnect.php
<?
require("userVariable.inc");
if (!mysql_connect($hostName, $dbUserName, $dbPassword)){
displayErrorPage("Cannot connect to ".$hostName."!");
exit;
}
@mysql_select_db($dbName) or die (mysql_error());
?>
filename: userVariable.inc
<?
/* Common MySQL connection variables */
$hostName="localhost"; // server name or hostname of your database
$dbUserName=""; // database username
$dbPassword=""; // database password
$dbName=""; // database name used in your mysql
?>
by the way u dont need to edit the file queryList.php and dbConnect.php coz it is standard bu u can make some revisions if u want to make it better.
in your userVariable.inc aasign the values of it which defines your server and your database
and in testQuery.php just change the query and the list of the values you want to grab and u want to display..
try it men and enjoy 🙂