im having a problem with some code. Im trying to set up pagination on my web page. The first part of the code works fine interms of displaying the results and if there are more then 1 record it shows the next button and how many pages there are.
But the problem is when i click on the next button or the back button the page comes out blank. With in the link is a $query variable which i cant make sense and that why it messes up. The site i got the code from uses the $query variable within its selects statement e.g
$results = mysql_query("SELECT * FROM your_table WHERE name LIKE '%". $query ."%' ORDER BY name ASC LIMIT $page, $limit");
the code i think the problem is occuring is the hyperlink that takes the user next. the code is this, as you can see it contains $query variable
<? echo "$display_block"; ?>
<br>
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>\n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");}
?>
The full code is as follows
if (!($limit)){
$limit = 1;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = mysql_query("select * from $table_name where job_sector = '$POST[job_sector]' AND region = '$POST[region]'"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows % $limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
?>
<?
// Now we can display results.
$results = mysql_query("select * from $table_name where job_sector = '$POST[job_sector]' AND region = '$POST[region]' ORDER BY date_posted ASC LIMIT $page, $limit"); // the query.
while ($row = mysql_fetch_array($results))
{
$candidate_id = $row['candidate_id'];
$what_they_do = stripslashes($row['what_they_do']);
$contact_phone = $row['contact_phone'];
$contact_name = $row['contact_name'];
$contact_email = $row['contact_email'];
$date_posted = $row['date_posted'];
$job_title = stripslashes($row['job_title']);
$candidate_description = stripslashes($row['cand_description']);
$short_summary = substr($candidate_description,0,150);
$location = stripslashes($row['location']);
$salary = $row['salary'];
$display_block .= "
<table width=80% border=0 cellspacing=1 cellpadding=0>
<tr bgcolor=#FFEBD7>
<td><a href=candidate_detail.php?candidate_id=$candidate_id>$what_they_do</a><br>$short_summary....<a href=candidate_detail.php?candidate_id=$candidate_id>[More]</a><br><strong>Anticipated Salary:</strong> $salary
</td>
<td width=26% valign=top>$location</td>
</tr>
</table>";
}
?>
<? echo "$display_block"; ?>
<br>
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>\n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");}
?>