I have been struggling with paged recordsets over the past couple of weeks and have got to a point where everything is now almost working.
At the moment all my results are being shown on one page, I have set the limit to 1 result per page. E.g at the moment the first result and second result are all shown on the same page. Instead 1 result should be on one page and the other result on the other page.
Here is the code, could anyone suggest any amendments?
<?php
include("datapager.php");
include("functions.php");
echo "<html>\n<head><title>Datapager Example</title></head>\n<body>\n";
// connect to some database
$conn = mysql_connect();
mysql_select_db("jobclever");
// the name of the table to get all results from in this example
$table = "job";
// the sql statement to query
$sql = "SELECT * FROM $table";
$sql_result = mysql_query($sql) or die('SQL Error');
$num_rows = mysql_num_rows($sql_result);
// create a new datapager passing it the connection and the sql
$dp = new datapager($conn, $sql);
// check to see if the page variable has been set
if( !isset( $page ) ){
$page = 1;
}
// set the number of results to get for each page
$pagesize = 1;
// execute the query with the current page and page size and return a mysql result
$res = $dp->execute($page, $pagesize);
// if the result is valid, use it like any other mysql result to output the records
if( $res ){
$fieldcount = mysql_num_fields($res);
}
if ($num_rows == 0){
echo 'Sorry your search did not return any results. Please return to the search page <a href=search.php>here</a>';
} else {
// output a header with information on the current page, number of pages and number of records
echo "<table align=center><tr><th colspan=$fieldcount>Showing page ".$dp->page." of ".$dp->pagecount;
echo ". Records ".((($dp->page -1) $dp->pagesize)+1)." to ".$dp->page $dp->pagesize." of ".$dp->recordcount."</th></tr><tr></table>\n";
while ($row = mysql_fetch_array($sql_result)){
$jobid = $row["jobid"];
$date= $row["date"];
$title = $row["title"];
$desc = $row["description"];
$location = $row["location"];
$salary = $row["salary"];
$industry = $row["industry"];
print "<table><tr><td>Job ID: $jobid</td></tr>";
print "<tr><td>Date:";
echo fixDate($date);
Print "</td></tr>";
print "<tr><td>Title: $title</td></tr>";
print "<tr><td>Description: $desc</td></tr>";
print "<tr><td>Location: $location</td></tr>";
print "<tr><td>Salary: $salary</td></tr>";
print "<tr><td>Industry: $industry</td></tr></table>";
print "To apply please click <a href=result_details.php?jobid=$jobid>here</a>\n";
}
// output a footer containing links to show previous / next and all other pages of results
echo "<tr><td colspan=$fieldcount><table width='100%'><tr>\n";
echo "<td width='33%' align=left>".$dp->prevpage("<a href='testdatapager.php?page=%page%'>Previous</a>\n");
echo "</td><td width='33%' align=center>".$dp->pagelinks("<a href='testdatapager.php?page=%page%'>%page%</a>", "%page%");
echo "</td>\n<td width='33%' align=right>".$dp->nextpage("<a href='testdatapager.php?page=%page%'>Next</a>");
echo "</td></tr></table></td></tr></table>\n";
}
?>