Hi,
I'm looking at something I previously worked on where it counted the total number of results in the database and created the pagination for it. This particular code didn't give the 1 to 10 out of 25 records but I have something else that might help you.
As far as getting the total number of rows I used...
PHP Code:
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tbl_name"),0);
The rest of it is building the pagination links and it displays the total number of pages.
This I think is more what you're after...look down towards the bottom at the $a variable (most of the other stuff you may not need, but I posted so you could see it all together)
PHP Code:
// Build SQL Query
$query = "select * from your_table_name where..." //tailor to fit your needs
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// $trimmed is just a variable to trim the whitespace from my search query like $var = @$_GET['q'] ; $trimmed = trim($var);
if ($numrows == 0)
{
echo "Results<br>";
echo "<p>Your search for: "" . $trimmed . "" returned zero results</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
$query2 .= " limit $s,$limit";
$result2 = mysql_query($query2) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $trimmed . ""</p>";
// begin to show results set
echo "Results<br>";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$id = $row["id"];
$item1 = $row["item1"];
$item2 = $row["item2"];
echo "Display results here - use variables if you want to." ;
// THIS IS THE PART YOU WILL NEED
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
That may give you a little direction to go in. Hope it helps.
Bookmarks