Originally posted by el_kab0ng
I snag a total count from a database based on criteria by doing this:
$count = mysql_query("select count(*) from table where criteria = '$string'");
$count = mysql_fetch_array($count);
which, when doing this echo's the total count:
echo "$count[0]";
But how do I get the count to do something like:
"12 results found, displaying 1 - 10." [/B]
<? echo $numguest7[0]; . " Found" ?>
this will go above your sql conenction and statement
$limit=10; // rows to return
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=1;
}
now you need to add this to your sql statement
"SELECT * from table limit $offset,$limit";
then after you get your posts and echo or print everything else add this code at the end
#$limit=20;
$Prev = "Previous";
$Next = "Next";
//SQL STATEMENT AGAIN
$numresults=mysql_query("SELECT * from table ");
$numrows=mysql_num_rows($numresults);
if (empty($offset)) {
$offset=0;
}
echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=2>";
while ($row = mysql_fetch_array($result)) {
/*
echo "
<tr bgcolor=#CCCCCC><td><font color=#000000 size=1><b>$row[$result]</b></font></td></tr>
"; */
}
mysql_free_result($result);
echo "</table>";
$pages=intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;
}
echo "<span class='titel'>Pages: </span>";
for ($i=1;$i<=$pages;$i++) {
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset&select=$select&query=$query\" class='titel'>$i</a> \n";
}
if ($offset>1) {
$prevoffset=$offset-$limit;
print "<a href=\"$PHP_SELF?offset=$prevoffset&select=$select&query=$query\" class='titel'>$Prev</a> \n";
}
if ($numrows>($offset+$limit)) {
$nextoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$nextoffset&select=$select&query=$query\" class='titel'>$Next</a><p>\n";
}
?>
this is some sample code, but this should give you an idea how it works