Re: boolean search
| From: | jtjohnston | Date: | Tue, 06 Nov 2001 08:21:39 +0000 |
| Subject: | Re: boolean search | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73559@lists.php.net to get a copy of this message | ||
> I'm looking for a piece of code that can do a boolean search
http://www.collegesherbrooke.qc.ca/languesmodernes/ccl/match.php
Click on boolean.
Search for "john and stockdale" and you get:
YR like "%john%" YR like "%AND%" YR like "%stockdale%"
I needed to flush out the %AND% and make it a boolean AND so I ... :
$where = str_replace("YR like \"%AND%\"", "+", $where);
$where = str_replace("YR like \"%OR%\"", "-", $where);
which gave me:
YR like "%john%" + YR like "%stockdale%"
But the search still produced 0 entries.
Is + and - the right syntax?
Eventually I will be searching in about 26 fields ANDing and ORing.
Advice? Suggestions?
Here is the code:
------------------snip---------------------
$where = "";
$words = explode(" ", $searchenquiry);
foreach ($words as $val)
{
if (strlen($where) > 0) $where .= " $type ";
$where .= "YR like \"%$val%\"";
}
echo "$where<br>";
$where = str_replace("YR like \"%AND%\"", "+", $where);
$where = str_replace("YR like \"%OR%\"", "-", $where);
echo "$where<br>";
$news = mysql_query('select * from ccl where '.$where.' order by YR desc');
$mycounter = 0;
while ($personne = mysql_fetch_object($news))
{
$mycounter++;
}
echo "\t<font color=blue face=\"arial,helvetica\" size=3><b>Your search
found
$mycounter \"<font color=black>$match</font>\"
entries!</b></font>\n\t<hr>\n\n";
echo "<ul>";
$news = mysql_query('select * from ccl where '.$where.' order by YR desc');
while ($personne = mysql_fetch_object($news))
{
echo "<li>Author: ".$personne->YR."</li>";
}
echo "</ul>";
}
------------------snip---------------------
> So your code would look like?
>
> $news = mysql_query('select * from news_elsst01 where '.$where.'order by
> news_title desc');
>
> When I type "john AND mary", it explodes it into 3 different $words?
>
> Can you explain what $type is?
>
> I'm still learning, and only really half learned Perl, so if you could indulge
> me, I'll never know/learn unless I ask:
>
> What does "foreach ($words as $val)" means? ... What kind of operator is
> "as"?
>
> Thanks,
>
> John
>
> > The way I've done this in the past is to do something like:
> > // assume AND/OR coming in in $type
> > // $searchme is another input
> > $where = "";
> > $words = explode(" ", $searchme);
> > foreach ($words as $val)
> > {
> > if (strlen($where) > 0) $where .= " $type ";
> > $where .= "field like '%$val%'";
> > }
> > // now tap $where on to your sql statement
> >
> > -----Original Message-----
> > From: jtjohnston [mailto:jtjohnston.delete@courrier.usherb.ca]
> > Sent: Tuesday, November 06, 2001 4:41 PM
> > To: php-general@lists.php.net
> > Subject: [PHP] boolean search
> >
> > Hi,
> >
> > I'm looking for a piece of code that can do a boolean search
> > from an <input type=text name=searchme>.
> >
> > Therefore, if the user types "AND" it will perform boolean
> >
> > <input type=text name=searchme value="john AND mary">
> >
> > Anything available?
> >
> > John
> >
> > ----------------------------------------------------------------------------
> > ---------------------------------------------
> >
> > This is what I am doing so far:
> >
> > $lien = mysql_pconnect("localhost","username","password");
> > mysql_select_db("database",$lien);
> >
> > $news = mysql_query('select * from news_elsst01 where
> > news_title like
> > "%'.$searchenquiry.'%" order by news_title desc');
> >
> > $mycounter = 0;
> > while ($personne = mysql_fetch_object($news))
> > {
> > $mycounter++;
> > }
> >
> > echo "\t<font color=blue face=\"arial,helvetica\"
> > size=3><b>Your search
> > found $mycounter entries!</b></font>\n\t<hr>\n\n";
> >
> > $news = mysql_query('select * from news_elsst01 where
> > news_title like
> > "%'.$searchenquiry.'%" order by news_title desc');
> >
> > while ($personne = mysql_fetch_object($news))
> > {
> > echo "<li><a
> > href=\"".$personne->news_filename."\"><font
> > size=3
> > color=red
> >
> >
> > face=\"arial,helvetica\"><B>".$personne->news_title."</B></font></a>
> > (<font size=1 color=Blue face=\"arial,helvetica\">By:
> > ".$personne->news_credit."</font>)<br><font size=2 color=black
> > face=\"arial,helvetica\">";
> >
> > //substr($personne->news_intro, 0, 200) // letters
> > $words = explode(" ", $personne->news_intro); //words
> >
> > for ($i = 0; $i <= 40; $i++)
> > {
> > echo "$words[$i] ";
> > }
> > echo " ...</font></a>\n\n";
> > }