how to display 10 results per page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrpatel
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2003 2:48 pm

how to display 10 results per page?

Post by mrpatel »

Hi all,
After running a query, I am getting about 1000 results. How do I display 10 results per page with a "previous" and "next" links at the bottom using PHP? Something like Google.com. Any kind of help is appriciated.


Thanks a lot,
Mehul
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Mrpatel, what you are looking for is called pagination (The act or process of numbering results per page and also the characters used in numbering the pages with a page number; system of numbering pages), now I wrote a pagination class file awhile back if you need it, but first see the pagination tutorial, hope this helps.
mrpatel
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2003 2:48 pm

Post by mrpatel »

Hi Kriek,
Thanks for your quick reply. I tried the "pagination" tutorial, but it works partly. It does limit the results to 10 and displays the links at the bottom, but it always displays the very first page no matter what page number you click on...!! Am i missing anything?


Thanks Again,
Mehul
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

mrpatel
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2003 2:48 pm

Post by mrpatel »

Hi macewan,
The same thing happenes even with this example also..!! Could someone please point out whats wrong with the following code?


<?php
include("../iDBfunctions.php");
if ($conn_id=DBopen()) {
?>

<html>
<body>
<h3>View All Personnel</h3>

<table align='left'cellspacing = "15">
<tr BGCOLOR="Silver">
<td align='center'><b>ID #</b></td>
<td align='center'><b>Rank</b></td>
<td align='center'><b>Title</b></td>
<td align='center'><b>Last Name</b></td>
<td align='center'><b>First Name</b></td>
<td align='center'><b>M.I.</b></td>
<td align='center'><b>suffix</b></td>
</tr>
<?php

$pagelimit = 50;
$query_count = "SELECT * FROM Personnel";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
$pagenums = ceil ($totalrows/$pagelimit);

if( empty($page) ){
$page = 1;
}
// create a start value
$start = ($page-1) * $pagelimit;

// blank matches found
echo "<b>" . $totalrows . " matches found</b><br>\n";

// Showing Results 1 to 1 (or if you're page limit were 5) 1 to 5, etc.
$starting_no = $start + 1;

if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}


echo "Results $starting_no to $end_count shown.<br>\n";

// create dynamic next, previous, and page links


if ($totalrows - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
$var2 = $totalrows - $end_count;
}

$space = "&nbsp;";

// previous link (make sure to change allpersonnel.php to the name of your page)
if ($page>1) {
echo "« <a href='allpersonnel.php?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}

// dynamic page number links (make sure to change allpersonnel.php to the name of your page)

for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='allpersonnel.php?page=$i' class=main>$i</a>";
}
else {
echo " <b class='red'>$i</b>";
}
}


// next link (make sure to change allpersonnel.php to the name of your page)

if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='allpersonnel.php?page=".($page+1)."' class=main>Next " . $var2 . "</a> »";
}


$cur= mysql_query( "SELECT Personnel.*, Rank.rank
FROM Personnel, Rank, Rank_Assoc r
WHERE Personnel.person_id = r.personnel_id
and r.rank_id = Rank.rank_id
LIMIT $start, $pagelimit");

if (!$cur) {
exit("Error in query");
}

while ($row=mysql_fetch_array($cur, MYSQL_ASSOC)) {
$per_id = $row["id_num"];
$per_rank = $row["rank"];
$per_title = $row["title"];
$per_last = $row["last_name"];
$per_first = $row["first_name"];
$per_mi = $row["middle_initial"];
$per_suffix = $row["suffix"];


print "<tr BGCOLOR='E7E7EF'>";
$pid=$row["person_id"];
print "<td align='center'> <a href=viewperson.php?id=$pid>$per_id</a></td>";
print "<td align='center'>$per_rank</td>";
print "<td align='center'>$per_title</td>";
print "<td align='center'>$per_last</td>";
print "<td align='center'>$per_first</td>";
print "<td align='center'>$per_mi</td>";
print "<td align='center'>$per_suffix</td>";
print "</tr>";

}

?>
</table>



</body>
</html>

<?php
DBclose($conn_id);
}
?>


Thanks a lot,
Mehul
Post Reply