Jump to content

Recommended Posts

I am working with an older script that I did not write.  It is working fine, but I want to convert it to a Boolean search.  I am a real newbie and this script seems to have some weirdness that is throwing me in addition to trying to set up a complicated search.  It is at the point where it will do single word searches and searches for items inside quotations, but it is ignoring AND/NOT/OR qualifiers. I have read manuals and searched the web for help on this and I have gotten conflicting information and/or something that didn't work (I think because of the way this query was written).  I feel like the solution will be obvious once I see it, but I am lost in the forest right now...

 

Here is the code:

<?php
if (isset($_GET['words'])) $words = $_GET['words'];
else $words = '';

echo "
<!--div class='searchExamples'>
<h3>Search examples:</h3>
<p><em>apple banana</em> ← find <strong>any</strong> of the words</p>
<p><em>apple <strong>+</strong>banana</em> ← results <strong>must</strong> include <em>banana</em></p>
<p><em>apple <strong>-</strong>banana</em> ← results <strong>can't</strong> include <em>banana</em></p>
<p><em>appl<strong>*</strong></em> ← find <strong>any</strong> word starting with <em>appl</em></p>
<p><em><strong>"</strong>apple juice<strong>"</strong></em> ← find this <strong>exact phrase</strong></p>
</div-->

<p>You can search the titles, authors, and abstracts of every article by entering keywords here:</p>
<form action='search.php' method='get'><p><input name='words' class='textfield' value='$words'><input type='submit' value='Search'><br>(For best results, try using spaces instead of punctuation marks such as hyphens.)</p></form>
<!-- p>To refine your search, see the examples to the right.</p -->";


if (isset($_GET['words'])) {
echo '<h2>Search results:</h2>';
$words = mysql_real_escape_string($_GET['words']);
$query = "SELECT `volume`,`number`,`year`,`title`,`authors`,`abstract`,MATCH (title,authors,abstract) AGAINST ('$words' IN BOOLEAN MODE) AS `score` FROM `articles` WHERE MATCH (title,authors,abstract) AGAINST ('$words' IN BOOLEAN MODE) ORDER BY `score` DESC;";

//error_log($query);
$result = mysql_query($query);

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$dns = mysql_real_escape_string(gethostbyaddr($_SERVER['REMOTE_ADDR']));
global $accountID;
if ($accountID===FALSE) $account = 'NULL';
else $account = '"'.mysql_real_escape_string($accountID).'"';
mysql_query("INSERT INTO `searches` (`timestamp`,`query`,`ip`,`dns`,`account_id`,`results`) VALUES (NOW(),'$words','$ip','$dns',$account,'".mysql_num_rows($result)."');");

if (mysql_num_rows($result)==0) {
	echo '<p>Sorry, your search returned no matches.  Try rephrasing your search words.</p>';
} else {
	$data = array();
	$max = 0;
	while ($row = mysql_fetch_assoc($result)) {
		$data[] = $row;
		$max = max($max,$row['score']);
	}
	echo '<table class="results"><tr><th>Article</th><th>Abstract</th></tr>';
	foreach ($data as $row) {
		$wordsSplit = explode(' ',$words);
		$wordsSplit = array_unique($wordsSplit);
		$title = htmlentities($row['title']);
		$authors = htmlentities($row['authors']);
		$abstract = htmlentities($row['abstract']);
		foreach ($wordsSplit as $thisWord) {
			$aword = preg_replace('|[^a-z]|i','',$thisWord);
			if ($aword!='') {
				$title = preg_replace("|($aword)|i",'<span>\1</span>',$title);
				$authors = preg_replace("|($aword)|i",'<span>\1</span>',$authors);
				$abstract = preg_replace("|($aword)|i",'<span>\1</span>',$abstract);
			}
		}
		if ($abstract=='') $abstract = 'No abstract available';
		echo '<tr><td class="articleInfo"><p class="issue">'.issueString($row['volume'],$row['number'],$row['year']).'</p><p><cite>'.$title.'</cite></p><p>'.$authors.'</p></td><td class="abstract">'.$abstract.'</td></tr>';
	}
	echo '</table>';
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/261120-converting-old-script-to-boolean/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.