Log in

View Full Version : Making a page with limited results



Schmoopy
03-14-2009, 12:14 AM
Hi, just as many search engines will say "Showing 1 - 10 results of x", I'm trying to implement this on one of my pages but am stumped as to how I could go around it.

I was thinking by using the page number they're on and then putting that into the query like if they're on page 2, "SELECT * FROM appointments LIMIT $pagenum, 10";

$pagenum would already have 9 added to it so it would basically get that page number, +9 to it and then look in the database for it.

page = 2 (page = 11) LIMIT 11, 10 - Show 10 records starting with row #11.

I'm just trying to figure out how to figure out how many pages will need to be generated and how to code it all.

Here's what I have:



if (mysql_num_rows($query) > 10)
{
echo "<div class=\"pagesel\">";
$i = 1;
$page = 2;
echo "Page: ";
while ($i < mysql_num_rows($query))
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . $page ."\">" . $page . "</a> ";
$i+= 10;
$page++;
}
echo "</div>";
}


This code works fine but it's the part where I have to only show the first 1 - 10 results and have the code above work also. If I use LIMIT 10 then the mysql_num_rows evaluates to 10, meaning that there are no more pages to put.

I just sort of got lost around here, anyone who can help?

djr33
03-14-2009, 01:17 AM
http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

?foru
03-14-2009, 01:48 AM
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...

// 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)

// 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: &quot;" . $trimmed . "&quot; 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: &quot;" . $trimmed . "&quot;</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 "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
Prev 10</a>&nbsp&nbsp;";
}

// 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 "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</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.

Schmoopy
03-14-2009, 02:41 AM
http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

Just what I was looking for :)

?foru
03-14-2009, 07:17 PM
Which is essentially what I posted just in another format...(used for search results)

echo "<p>Showing results $b to $a of $numrows</p>";

first part of code posted...same idea as paging tutorial...uses COUNT ceil and all

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tbl_name"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);