Entry
How can display only limited records in a pge ,where the result from a select query?
Feb 19th, 2008 22:27
dman, Ajesh Babu, Knud van Eeden, http://sturly.com
//DB Connection
function pagenav() {
global $limit,$offset,$numpage;
echo "
<TABLE CELLPADDING=0 BORDER=0 CELLSPACING=5 WIDTH=100>
<TR>
<TD ALIGN=RIGHT>";
if ($offset>=$limit) {
$newoff=$offset-$limit;
echo "<A HREF=\"$PHP_SELF?offset=$newoff\">
<-- PREV</A>
</TD>";
} else {
echo "<-- PREV";
}
echo " </TD>
<TD ALIGN=LEFT>";
if ($offset!=$limit*($numpage-1)) {
$newoff=$offset+$limit;
echo "<A HREF=\"$PHP_SELF?offset=$newoff\">
NEXT--></A>
</TD>";
}else{
echo "NEXT--></TD>";
}
echo "</TR>
</TABLE>";
} // END FUNCTION
// set this to the number of results you wish on each page
$limit=10;
// if no offset has been passed, offset should be 0
if (!$offset) $offset=0;
// NOTE: if a pipe (|) may be in the value
// of $one or $two, use a different delimiter
$result=mysql_query(//Your Count Query);
list($numrec)=mysql_fetch_row($result);
#calc num pages
$numpage=intval($numrec/$limit);
if ($numrec%$limit) {
$numpage++; // add one page if remainder
}
$result=mysql_query (//Your data Query. "limit $offset,$limit");
//<!-- HTML headers and other non-relevent stuff -->
// now you can display the results returned
while ($data=mysql_fetch_array($result)) {
// include code to display results as you see fit
}
if ($numpage>1) {
pagenav();
print "<P>";
}
You can include Where condition in your query.