Jump to content

MySQL Search... Getting rid of the "normal" code for a boolean only engine


suttercain

Recommended Posts

Hi guys and good Friday evening... I had to come on and get my fix...

 

Anyway, I got the tutorial that PHPFreak did for his MySQL search engine. In his code he gives the option to the user to select either a "normal" search or a "boolean" search.

 

I got the code working and it's great! My question is how do I get rid of the Mode (normal and boolean) menu  and have it only do a boolean search?

 

I have tried and tried again but can't get it. I love the code but don't want the mode.... yizzode

 

<?php
require ('get_connected.php');

// 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
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');

switch($cmd)
{
  default:
    
    searchForm();
  
  break;
  
  
  case "search":
    searchForm();
    ;
    
    $searchstring = mysql_escape_string($_GET['words']);
    switch($_GET['mode'])
    {
      case "normal":
        $sql = "SELECT mytable_id, mytable_title, mytable_caption, mytable_dts, 
               MATCH(mytable_title, mytable_caption, mytable_full_body) 
               AGAINST ('$searchstring') AS score FROM mytable 
               WHERE MATCH(mytable_title, mytable_caption, mytable_full_body) 
               AGAINST ('$searchstring') ORDER BY score DESC";
      break;
      
      case "boolean":
        $sql = "SELECT title, issue_number, cover_date, comic_id,
               MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM comics 
               WHERE MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
      break;
    } 
    
    // echo $sql;
    
    $result = mysql_query($sql) or die (mysql_error());
    $hello = mysql_num_rows($result);
echo "<h3>" . $hello . " Matching Results:</h3>";
    while($row = mysql_fetch_object($result))
    {
  echo $row['title'];
      echo "<a href='http://www.supermandatabase.com/view_comics.php?id=" . stripslashes(htmlspecialchars($row->comic_id)) . 
  "'><strong>".stripslashes(htmlspecialchars($row->title)). "</strong></a>";
  echo '<strong> #'.stripslashes(htmlspecialchars($row->issue_number)).'</strong><br />';
      echo 'Cover Date: '.date('F, Y', strtotime($row->cover_date)).'<br />';
      echo '<p>'.stripslashes(htmlspecialchars($row->mytable_caption)).'</p>';
      echo '<hr size="1" />';
    }
  break;
}
?>

 

Any help or suggestions?

 

Thanks.

 

Shannon

Here it is.. totally untested and ready to go!  What I did was

 

1.  Delete code to process $_GET['mode'] near the start

2.  Delete mode option from the form

3.  Delete switch statement that chooses between normal and boolean mode.

 

<?php
require ('get_connected.php');

// 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:
    
    searchForm();
  
  break;
  
  
  case "search":
    searchForm();
    ;
    
    $searchstring = mysql_escape_string($_GET['words']);

    $sql = "SELECT mytable_id, mytable_title, mytable_caption, mytable_dts, 
               MATCH(mytable_title, mytable_caption, mytable_full_body) 
               AGAINST ('$searchstring') AS score FROM mytable 
               WHERE MATCH(mytable_title, mytable_caption, mytable_full_body) 
               AGAINST ('$searchstring') ORDER BY score DESC";
    
    // echo $sql;
    
    $result = mysql_query($sql) or die (mysql_error());
    $hello = mysql_num_rows($result);
echo "<h3>" . $hello . " Matching Results:</h3>";
    while($row = mysql_fetch_object($result))
    {
  echo $row['title'];
      echo "<a href='http://www.supermandatabase.com/view_comics.php?id=" . stripslashes(htmlspecialchars($row->comic_id)) . 
  "'><strong>".stripslashes(htmlspecialchars($row->title)). "</strong></a>";
  echo '<strong> #'.stripslashes(htmlspecialchars($row->issue_number)).'</strong><br />';
      echo 'Cover Date: '.date('F, Y', strtotime($row->cover_date)).'<br />';
      echo '<p>'.stripslashes(htmlspecialchars($row->mytable_caption)).'</p>';
      echo '<hr size="1" />';
    }
  break;
}
?>

[/code]

Hello,

 

Thanks for writing back. I had tried that but when I run the search after I get 0 results where before with the same keyword I got 83

 

<?php
// Full-Text Search Example
// Conect to the database.
require ('get_connected.php');
$searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
  echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
  echo '<input type="hidden" name="search" value="search" />';
  echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
  echo '<input type="submit" value="Search"/>';
  echo '</form>';


if(isset($_GET['words'])) {

        $sql = "SELECT title, issue_number, cover_date, comic_id,
               MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM comics 
               WHERE MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
    
    $result = mysql_query($sql) or die (mysql_error());
    $hello = mysql_num_rows($result);
echo "<h3>" . $hello . " Matching Results:</h3>";
    while($row = mysql_fetch_object($result))
    {
  echo $row['title'];
      echo "<a href='http://www.supermandatabase.com/view_comics.php?id=" . stripslashes(htmlspecialchars($row->comic_id)) . 
  "'><strong>".stripslashes(htmlspecialchars($row->title)). "</strong></a>";
  echo '<strong> #'.stripslashes(htmlspecialchars($row->issue_number)).'</strong><br />';
      echo 'Cover Date: '.date('F, Y', strtotime($row->cover_date)).'<br />';
      echo '<p>'.stripslashes(htmlspecialchars($row->mytable_caption)).'</p>';
      echo '<hr size="1" />';
    }
}
?>

Hi Bthrel,

 

I ran this code, and got 0 results from my search.

 

<?php
require ('get_connected.php');

// 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:
    
    searchForm();
  
  break;
  
  
  case "search":
    searchForm();
    ;
    
    $searchstring = mysql_escape_string($_GET['words']);

    $sql = "SELECT title, issue_number, cover_date, comic_id,
               MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM comics 
               WHERE MATCH(title, issue_number, cover_date, comic_id) 
               AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
    
    // echo $sql;
    
    $result = mysql_query($sql) or die (mysql_error());
    $hello = mysql_num_rows($result);
echo "<h3>" . $hello . " Matching Results:</h3>";
    while($row = mysql_fetch_object($result))
    {
  echo $row['title'];
      echo "<a href='http://www.supermandatabase.com/view_comics.php?id=" . stripslashes(htmlspecialchars($row->comic_id)) . 
  "'><strong>".stripslashes(htmlspecialchars($row->title)). "</strong></a>";
  echo '<strong> #'.stripslashes(htmlspecialchars($row->issue_number)).'</strong><br />';
      echo 'Cover Date: '.date('F, Y', strtotime($row->cover_date)).'<br />';
      echo '<p>'.stripslashes(htmlspecialchars($row->mytable_caption)).'</p>';
      echo '<hr size="1" />';
    }
  break;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.