guys, here is the code that I used to do my pagination
<FORM method="get" action="searching.php">
<INPUT size=40 name=searchterm type=text>
<INPUT type="submit" name="submit" value="search">
searching.php code:
<?php
$searchterm = $_GET["searchterm"];
trim($searchterm);
if (!$searchterm)
{
echo " You have not entered a search.<br><br>";
exit;
}
$searchterm = addslashes($searchterm);
// Database Connection
include 'db.php';
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 5;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM BusinessName
WHERE business_name LIKE '%$searchterm%'
ORDER BY business_name
LIMIT $from, $max_results");
$num_results = mysql_num_rows($sql);
echo "Your search for <b>\"" .$searchterm. "\"</b> returns <b>\"".$num_results."\"</b> records</p>";
for ($i=0; $i <$num_results; $i++) {
$row = mysql_fetch_array($sql);
// Build your formatted results here.
echo "<p><strong>".($i+1).". ";
echo "<a href=\"http://www.help.com\">";
echo htmlspecialchars( stripslashes($row["business_name"]));
echo "</a></strong><br> ";
echo "<a href=\"http://www.maporama.com\" target=\"_blank\">Map it!</a>";
echo "
<a href=\"http://{$row['webpagelink']}\" target=\"_blank\">";
echo htmlspecialchars (stripslashes($row["webpagelink"]));
echo "</a><br />";
echo htmlspecialchars (stripslashes($row["business_address"]));
echo ", ";
echo htmlspecialchars (stripslashes($row["bus_city"]));
echo ", ";
echo htmlspecialchars (stripslashes($row["businessacct_state_country"]));
echo " ";
echo htmlspecialchars (stripslashes($row["bus_zip"]));
echo "<br>Phone:<strong> ";
echo htmlspecialchars (stripslashes($row["business_phone"]));
echo "</strong> Fax: ";
echo htmlspecialchars (stripslashes($row["business_fax"]));
echo "</p>";
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM BusinessName"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page ";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['searching.php']."?page=$prev\"><< Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['searching.php']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['searching.php']."?page=$next\">Next >></a>";
}
echo "</center>";
?>
when i hit the "next" or "2" it just shows me a page that tells me that You have not entered a search, which I did up there. The reason for this echoing is to tell the user that search was empty.
I'm trying to develope a search engine to search for specific words, not just what is in the database.
Further, I took out
trim($searchterm);
if (!$searchterm)
{
echo " You have not entered a search.<br><br>";
exit;
}
and it allows me to see my "Previous, Next" etc. In the record prior to the "Previous, Next", it shows 5 results.
1,2,3,4,5 when I hit Next or "2", it starts at 1 again.