I am currently trying to return results in a paged recordset format.
At the moment I have it set to return 1 result per page and know the search I am executing should return 2 results therefore 2 pages should be returned.
The problem occurs when the search is executed all the results are being returned on one page. Does anyone know how I can amend my code to distribute the results to the predefined page size per page.
Here is the code I am using:
<?php
include("datapager.php");
include("functions.php");
// 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";
}
?>