Hi all,
I have newstable
===============
newstitle
newsdetails
newslocation
===============
Search form
===================
Search: <textbox>
location: <dropdownlist>
====================
I need to fetch records based on multiple search criteria entered by
user in search box and location.
For example: user inputs different search string separated by comma, I
want to treat each value as OR in my SQL logic
Supposing, user entered: <rat, max, kill, football>
I wanted something like
===========================
SELECT * FROM newstable
WHERE location = [user selected location]
AND
WHERE (either [newstitle] or [newsdetails] like %<serach term1>%)
OR
WHERE (either [newstitle] or [newsdetails] like %<serach term2>%) and so on
===========================
How do we do that with DB_Table_Select?
With my current approach it is working but has a flaw
My solution: (code snip from Model)
=========================
if ($locstr !== "Any")
{
$select->where('newslocation = ?', $locstr);
}
if ($reqstr !== "")
{
$tokstring = explode(",", $reqstr);
foreach ($tokstring as $token)
{
$select->orwhere('newstitle like ?', "%". trim($strtok) . "%");
$select->orwhere('newdetails like ?', "%". trim($strtok) . "%");
}
}
=========================
This works but with a flaw (in orwhere)
1. From above code, newslocation gets the priority. All the records
matching newslocation gets selected and second part of the logic is
ignored.
2. If I move my request string logic part at first step, it fetches
the matching tiles whether or not its is from the selected location.
Some suggestion is greatly appreciated.
many thanks
--
=======================
Registered Linux User #460714
Currently Using Fedora 8, 10
=======================