pagination, cant pass variable (i think!)

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
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

pagination, cant pass variable (i think!)

Post by vegas22 »

hello again,

im trying to get this pagination script to work with my search form.

at the moment it works fine until the user click "next page" "last page" etc

when they click this it load up the "We could not detect your search term. Please re-enter it in ... etc"

any ideas?

Code: Select all

  <?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable
  
  if ($trimmed == "")
  {
  echo "We could not detect your search term. Please re-enter it in the box below or view all our jobs by clicking <a href=\"welfare-jobs.php\">here</a>.
  <br>
  <form name=\"form\" action=\"keyword.php\" method=\"get\">
    <input type=\"text\" name=\"q\" size=\"25\" />
   <input type=\"image\" src=\"search.jpg\" alt=\"Submit button\" name=\"Submit\" value=\"Search\"  />
</form>
  
  
  </div>

</div>

<div id=\"footer\">
  <div id=\"copyright\">
  		<div style=\"width:450px; height:30px; float:left; text-align: left;\">Copyright 2010-2011 Welfare People<br />
        <a href=\"privacy-policy.php\">Privacy Policy</a> : <a href=\"contact.php\">Contact</a></div>
  		<div style=\"width:450px; height:30px; float:right; text-align: right;\">Created by <a href=\"http://www.sheffielddesignservices.com\" target=\"_blank\">Web Design Sheffield</a><br />
  		  Hosted by <a href=\"http://www.squirrelhosting.co.uk\" target=\"_blank\">Sheffield Hosting</a></div>
  </div>

</div>




</div>

</body>
</html>

";
  exit;
  }



  
  ?>

  

<?php
// database connection info
$conn = mysql_connect('localhost',******','******') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('******',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM jobmodule WHERE description like \"%$trimmed%\" order by postdate";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];




if ($numrows == 0)
  {
  echo "Sorry, your search: "" . $trimmed . "" returned zero results. Please try another term it in the box below or view all our jobs by clicking <a href=\"welfare-jobs.php\">here</a>.
  <br>
  <form name=\"form\" action=\"keyword.php\" method=\"get\">
    <input type=\"text\" name=\"q\" size=\"25\" /></td>
   <input type=\"image\" src=\"search.jpg\" alt=\"Submit button\" name=\"Submit\" value=\"Search\"  />
</form>
  
  
  </div>

</div>

<div id=\"footer\">
  <div id=\"copyright\">
  		<div style=\"width:450px; height:30px; float:left; text-align: left;\">Copyright 2010-2011 Welfare People<br />
        <a href=\"privacy-policy.php\">Privacy Policy</a> : <a href=\"contact.php\">Contact</a></div>
  		<div style=\"width:450px; height:30px; float:right; text-align: right;\">Created by <a href=\"http://www.sheffielddesignservices.com\" target=\"_blank\">Web Design Sheffield</a><br />
  		  Hosted by <a href=\"http://www.squirrelhosting.co.uk\" target=\"_blank\">Sheffield Hosting</a></div>
  </div>

</div>




</div>

</body>
</html>

";
  exit;
  }
  
  
  
  
// number of rows to show per page
$rowsperpage = 1;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM jobmodule WHERE description like \"%$trimmed%\" order by postdate LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);




echo "Below are the results from your search. <br />
If you wish to search again please enter another term in the box below or view all our jobs by clicking <a href=\"welfare-jobs.php\">here</a>
<form name=\"form\" action=\"keyword.php\" method=\"get\">
    <input type=\"text\" name=\"q\" size=\"25\" />
   <input type=\"image\" src=\"search.jpg\" alt=\"Submit button\" name=\"Submit\" value=\"Search\"  />
</form>
<br /><div id=\"jobs-section\">


<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" >
	<tr>
		<td style=\"color:#FFF;\" >
		Job Title
		</td>
		<td style=\"color:#FFF;\">
		Contract
		</td>
		<td style=\"color:#FFF;\">
		Location
		</td>
		<td style=\"color:#FFF;\">
		Salary
		</td>
		<td style=\"color:#FFF;\">
		Posted Date</td>
        <td style=\"color:#FFF;\">
		More Info</td>
    </tr>
    
    <tr>
    <td colspan=\"6\" height=\"1\">
    </td>
    </tr>";

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {

  
// echo data " . $list['jobname'] . "
echo "<tr  bgcolor=\"$row_color\">
		<td>
		" . $list['jobname'] . "
		</td>
		<td>
		" . $list['contract'] . "
		</td>
		<td>
		" . $list['location'] . "
		</td>
		<td>
		" . $list['salary'] . "
		</td>
		<td>
		" . $list['postdate'] . "</td>
        <td>
		<a href=\"job.php?id=" . $list['id'] . "\">More Info</a></td>
    </tr>";
} // end while

echo "</table></div>";

/****** build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First Page</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage?'>Previous Page</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if 
} // end for

// if not on last page, show forward and last page links 
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page 
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next Page</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last page</a> ";
} // end if
/****** end build pagination links ******/

echo "</div>";
?>
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

Re: pagination, cant pass variable (i think!)

Post by vegas22 »

sussed it, for anyone interested or who get stuck on a similar problam i added "&q=$var" to
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last page</a> ";
Post Reply