Ok slight problem with my code! For some reason, no matter what my results are (if it's 1 page or many others), it always show the numbered links and Next link! Can someone help me out here? It's driving me nuts!
<?php
// Connect to the database.
$cnx = mysql_connect('localhost', 'root', 'pass') or die ("Could not connect");
mysql_select_db('main', $cnx) or die (mysql_error());
// Create the search function:
function searchForm()
{
// Re-usable form
// variable setup for the form.
$searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="hidden" name="cmd" value="search" />';
echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
echo '<input type="submit" value="Search" />';
echo '</form>';
}
// Create the navigation switch
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');
switch($cmd)
{
default:
echo '<h1>Search Database!</h1>';
searchForm();
break;
case "search":
searchForm();
echo '<h3>Search Results:</h3><br />';
$searchstring = mysql_escape_string($_GET['words']);
//Interviews query
// 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);
{
$sql = "SELECT name, date, description, content, url,
MATCH(name, date, description, content, url)
AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM interviews
WHERE MATCH(name, date, description, content, url)
AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC LIMIT $from, $max_results";
}
//Echo Interviews query
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<a href="'. $row->url .'">'.stripslashes(htmlspecialchars($row->name)).'</a><br />';
echo '<span class="text-small">';
echo date('F j, Y',strtotime($row->date)).'<br />';
echo '</span>';
echo stripslashes(htmlspecialchars($row->description)).'<br />';
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM interviews"),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><br />";
// Build Previous Link
if ($page != 1) {
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&cmd=$cmd&words=$searchstring\"><<</a> ";
}
for ($i = 1; $i <= $total_pages; $i++){
if ($page == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&cmd=$cmd&words=$searchstring\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&cmd=$cmd&words=$searchstring\">>></a> ";
}
echo "</center>";
break;
}
?>