HELP!! - Search
| From: | Matthew Cothier | Date: | Tue, 24 Jul 2001 13:16:27 +0000 |
| Subject: | HELP!! - Search | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-10705@lists.php.net to get a copy of this message | ||
Whats wrong with this?? Its not working.......
<?php include("search_bool.inc"); ?> <?php $connection = mysql_connect("$host","$dbusername","$dbpassword") or die ("Cannot connect to server."); $db = mysql_select_db("$dbname", $connection) or die ("Could not select database."); $sqlstring = "SELECT * FROM $searchsection WHERE ".makeboolean("keyword",$search); $result = mysql_query($sqlstring, $connection) or die ("Could not execute query."); if (!$result) { echo "<p>Could not get record."; } if(mysql_num_rows($result)) { $uid = 1; while($row = mysql_fetch_row($result)) { print("<font face=\"Verdana\" size=\"1\"><b><a href=\"#\" onclick=\"window.open('./view_ep.php3?news_id=$row[0]','','width=490,height=280');\">$row[1]</a></b><br><br>$row[2]</font></small><p>"); $uid++; } } ?>Search.php
## I don't even get these instructions!! *lol* ## usage: makeboolean("fieldname",$searchstring) ## ie: $sqlstring = "SELECT * FROM table WHERE ".makeboolean("field",$search); class Query { var $m_words; var $m_logic; function Query($logic, $words) {Search Boolean file I was told to use
$logic = " ".$logic." ";
$this->m_words = $words;
$this->m_logic = $logic;
}
};
class phpSQLbool {
var $m_Querys;
var $m_start;
var $m_offset;
var $m_wordmatch;
var $m_casematch;
function QueryGenerator($Querys, $wordmatch=false, $casematch=false) {
$this->m_Querys = $Querys;
$this->m_wordmatch = $wordmatch;
$this->m_casematch = $casematch;
$this->Restrict(0,0);
}
function Restrict($start, $offset) {
$this->m_start = $start;
$this->m_offset = $offset;
}
function GetRestrict(&$start, &$offset) {
$start = $this->m_start;
$offset = $this->m_offset;
}
function GetQueryString($searchfield) {
// $query_string = "SELECT $resultfield FROM $table WHERE ";
$query_string .= $this->BuildQueryString($table, $searchfield);
if ($this->m_start || $this->m_offset) {
$query_string .= "LIMIT $this->m_start";
if($this->m_offset)
$query_string .= ",$this->m_offset";
}
return $query_string;
}
function GetCountQueryString($table, $searchfield) {
$query_string = "SELECT count(*) FROM $table WHERE ";
$query_string .= $this->BuildQueryString($table, $searchfield);
return $query_string;
}
function BuildQueryString($table, $searchfield) {
$searchfields = explode(",", $searchfield);
if(!is_array($searchfields))
$searchfields=array($searchfield);
while ($searchfield = current($searchfields)) {
if (!$this->m_casematch)
$searchfield = $searchfield;
$firstquery = true;
reset($this->m_Querys);
while($query = current($this->m_Querys)) {
if ($firstquery)
$firstquery=false;
else if ($query->m_logic == "NOT")
$query_string .="AND ";
else
$query_string .=$query->m_logic;
$query_string .= $this->BuildWhereClause($searchfield, $query);
next($this->m_Querys);
}
if (next($searchfields))
$query_string .= "OR";
}
return $query_string;
}
function BuildWhereClause($searchfield, $query) {
while($word = current($query->m_words)) {
if (!$this->m_casematch)
$word = sql_regcase($word);
if ($this->m_wordmatch)
$search_type = "REGEXP '[[:<:]]".$word."[[:>:]]'";
else
$search_type = "REGEXP '$word'";
if ($query->m_logic == "NOT")
$query_string.="$searchfield NOT ";
else
$query_string.="$searchfield ";
$query_string.= $search_type;
if (next($query->m_words))
$query_string.=" OR ";
}
return $query_string;
}
};
function makeboolean($field, $keywords) {
$words = explode(" ", $keywords);
for ($i=0; $i < count($words); $i++) {
$query[] = new Query("AND", array(trim($words[$i])));
}
$querygen = new phpSQLbool($query);
$query = $querygen->GetQueryString($field);
return $query;
}
?>
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp