Pagination... Driving me Crazy!!!
Posted: Mon Sep 18, 2006 3:41 pm
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
OK, I have an HTML page, consisting of a form with one object inside - a text box, called SearchBox. The user presses a 'Submit' button, and then a script that searches a database, and returns results to the user in pages- 10 results per page. The correct number of results are returned, and the script correctly determines the number of pages there should be. Now, the problem is, whenever I try to click on page 2 of the results, I NEVER GET THERE! Page 1 displays again and again. In the address bar, the URL changes as follows:
here it is after I hit the search button, with the character 's' entered into the SearchBox :
/Search.php?SearchBox=s&Submit=Search
Now, I want to go to page 2, so I click on the '2' link:
/Search.php?SearchBox=s&Submit=Search?page=2
NOthing happened, except page 1 displyed again, I hit the '2' link again:
/Search.php?SearchBox=s&Submit=Search?page=2?page=2
One final time:
/Search.php?SearchBox=s&Submit=Search?page=2?page=2?page=2
If I keep clicking the '2' link, the script just keeps appending a ?page=2 to the end of the URL.
I can't understand why this is happening, hopefully someone here can help. I have tried everything I can think of, nothing helps. Below is my script.Code: Select all
<?php
// Get THE search variable from HTML form.
$searchTerm = @$_GET['SearchBox']; // This will retrieve what the user types into the search box.
$trimmed = trim($searchTerm); // Trim any whitespace from the user's typed text.
$resultsPerPage = 10; // Number of results to show per page.
$this_page = basename($_SERVER['REQUEST_URI']);
global $test;
// If there is no search term, tell the user.
if ($trimmed == "")
{
print "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($searchTerm) )
{
print "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE WHEN SITE GOES LIVE**
mysql_connect("localhost","admin","admin"); //(host, username, password)
//specify database - USER in this case** EDIT REQUIRED HERE **
mysql_select_db("USER") or die("Unable to select database"); //select which database we're using
// Build SQL Query - users_passwords is the name of the table in the USER database
$query = "select * from users_passwords where firstName like \"%$trimmed%\"
order by firstName";
$numresults= @mysql_query($query);
$num_rows=mysql_num_rows($numresults);
$num_pages = $num_rows / $resultsPerPage;
// If we have no results, offer a google search as an alternative
if ($num_rows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
exit;
}
// get current page
$page = $_GET['page'];
if(!ctype_digit($page)) { // ctype_digit checks if all chars in the strings are numeric. if yes, return true. If not, return false
}
$limit = $page * $resultsPerPage - ($resultsPerPage); // 0 when page is 1, 10 when page is 2, 20 when page is 3, etc.
// get results
$query .= " limit $limit,$resultsPerPage";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $searchTerm . ""</p>";
// begin to show results set
echo "Results";
// now you can display the results returned
echo "<TABLE width = 50% border = 1>";
echo "<TR><TD>First Name</TD><TD>Last Name</TD><TD>HomePage</TD></Tr>";
while ($row= mysql_fetch_array($result)) {
$firstName = $row["firstName"];
$lastName = $row["lastName"];
$userName = $row["userName"];
echo "<TR> <TD>$firstName</TD><TD>$lastName</TD><TD><A HREF=\"$userName\"> dummy </A></TD></TR>";
}
echo "</TABLE>";
// $currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
print "Just printed table. Page is ".$page." and numpages is ".$num_pages." <BR>";
// display "previous" link if applicable
if($page > 1) {
$previous_page = $page--;
echo "<a href=\"$this_page?page=$previous_page\">« Previous </a>";
}
// display individual page links (pages with a block of 10 results)
for($i = 1; $i <= $num_pages; $i++) {
if($i == $page) {
echo "<strong>$i</strong> ";
}
else {
echo "<a href=\"$this_page?page=$i\">$i</a> ";
}
}
// display final page link if needed (page with a block of < 10 results)
if(($num_rows % $resultsPerPage) != 0) {
if($i == $page) {
echo "<strong>$i</strong> ";
}
else {
echo "<a href=\"$this_page?page=$i\">$i</a> ";
}
}
// display "next" link if applicable
if(($num_rows - ($resultsPerPage * $page)) > 0) {
$next_page = $page++;
echo "<a href=\"$this_page?page=$next_page\">Next »</a>";}
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]