As a newbie to php and mysql but an old foxpro programmer I am having problems with getting the pagination to work with mysql query result. Depending on code changes I get all of the records (50) in my test table when I am actually trying to get just (15) records that the query should return.
I am attaching the php code that responds to an html form page search that is captured with the GET code.
Any help would be greatly appreciated.
DickM
===============
<html>
<body>
<?php
//Make the connection.
$conn = mysql_connect ('localhost', 'username', 'password'); //OR die ('Count not connect to MySQL: '. mysql_error() );
// Select the database.
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('capweb', $conn);
if(!$status) die("Failed to select database!");
//get data passed from history_search.html form as entered by user
$search=$_GET["search_string"]; // Get the value from the html form
// how many rows to show per page
$rowsPerPage = 5;
// by default we show first page
$pageNum = 1;
// if $_GET[‘page’] defined, use it as page number
if(isset($_GET[‘page’]))
{
$pageNum = $_GET[‘page’];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
//Get query
$result = mysql_query(“SELECT cusid,cname,mycustomer,jobsite1,estimate FROM plnsdec WHERE cname LIKE ‘$search%’ ORDER BY cname”) or die(mysql_error());
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
// print the data found
echo "<table border='1'>
<tr>
<th>Customer ID</th>
<th>Company</th>
<th>My Customer</th>
<th>Jobsite</th>
<th>Estimate</th>
</tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['cusid'] . "</td>";
echo "<td>" . $row['cname'] . "</td>";
echo "<td>" . $row['mycustomer'] . "</td>";
echo "<td>" . $row['jobsite1'] . "</td>";
echo "<td>" . $row['estimate'] . "</td>";
echo "</tr>";
}
echo "</table>";
// how many pages we have when using paging?
$maxPage = ceil($num_rows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER[‘PHP_SELF’];
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\“$self?page=$page\”>[Prev]</a> ";
$first = " <a href=\“$self?page=1\”>[First Page]</a> ";
}
else
{
$prev = ’ '; // we’re on page one, don’t print previous link
$first = ’ '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\“$self?page=$page\”>[Next]</a> ";
$last = " <a href=\“$self?page=$maxPage\”>[Last Page]</a> ";
}
else
{
$next = ’ '; // we’re on the last page, don’t print next link
$last = ’ '; // nor the last page link
}
// print the navigation link
echo $first . $prev . " Showing page $pageNum of $maxPage pages " . $next . $last;
?>
</body>
</html>