Hello,
I got mysql fulltext search. I want to add page navigation to it. But can't figure out how to make it. I am looking for some help.
Here is the code:
PHP Code:
<html>
<head><title>Search</title></head>
<body>
<?php
// Full-Text Search Example
// Conect to the database.
$cnx = mysql_connect('bla', 'bla', 'bla') or die ("Could not connect");
mysql_select_db('bla', $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'])) : '');
$normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' );
$boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' );
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 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select> ';
echo '<input type="submit" value="Search" />';
echo '</form>';
}
// Create the navigation switch
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$max_results = 10;
$from = (($page * $max_results) - $max_results);
$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']);
switch($_GET['mode'])
{
case "normal":
$sql = "SELECT item_content, item_title, item_link,
MATCH(item_content, item_title, item_link)
AGAINST ('$searchstring') AS score FROM items
WHERE MATCH(item_content, item_title, item_link)
AGAINST ('$searchstring') ORDER BY score DESC LIMIT $from, $max_results";
break;
case "boolean":
$sql = "SELECT item_content, item_title, item_link,
MATCH(item_content, item_title, item_link)
AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM items
WHERE MATCH(item_content, item_title, item_link)
AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC LIMIT $from, $max_results";
break;
}
echo $sql;
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<strong>Title: '.stripslashes(htmlspecialchars($row->item_title)).'</strong><br />';
echo 'Score:'. number_format($row->score, 1).'';
echo '<p>'.stripslashes(htmlspecialchars($row->item_link)).'</p>';
echo '<hr size="1" />';
}
break;
}
?>
</body>
</html>
And the navigation script:
PHP Code:
<?php
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$max_results = 10;
$from = (($page * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo $row['title']."<br />";
}
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0);
$total_pages = ceil($total_results / $max_results);
echo "<center>Select a Page<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
I already got the limit added so it displays 10 results.
But i can't figure out to add the navigation part to the search part. So the 2 are combined.
Bookmarks