Forum Moderators: open
explode($searchstring,char(32)); $return = "SELECT id, o_companyid, o_staffid, c_source, p_name, c_company, c_alias, p_email1, p_email2, c_location, c_address1, c_address2, c_address3, c_city, c_regio, c_postal, c_body, ...MANY MORE...
FROM addx
WHERE
id LIKE '%%$search%%'
OR o_companyid LIKE '%%$search%%'
OR o_staffid LIKE '%%$search%%'
OR c_source LIKE '%%$search%%'
OR p_name LIKE '%%$search%%'
OR c_company LIKE '%%$search%%'
OR c_alias LIKE '%%$search%%'
OR p_email1 LIKE '%%$search%%'
OR p_email2 LIKE '%%$search%%'
OR c_location LIKE '%%$search%%'
OR c_address1 LIKE '%%$search%%'
OR c_address2 LIKE '%%$search%%'
OR c_address3 LIKE '%%$search%%'
OR c_city LIKE '%%$search%%'
OR c_regio LIKE '%%$search%%'
OR c_postal LIKE '%%$search%%'
OR c_body LIKE '%%$search%%'
...MANY MORE...
$sort
";
//First make sure there's no double spaces
$term = preg_replace('/\s+/',' ',$_post['term']);
// Can't recall exact trim syntax ATM, reverting to regexps, sorry. :-)
$term = preg_replace('/^\s|\s$/','',$term);
$words = explode(' ',mysql_real_escape_string($term));
//
// Better off using a function to get these field names.
// You might skip numeric fields, like "id".
$fields = Array(
'o_companyid',
'o_staffid',
'c_source',
'p_name',
'c_company',
'c_alias',
'p_email1',
'p_email2'
// etc.
);
$where=NULL;
foreach ($fields as $field) {
$andwhere=NULL;
// only add an OR if $where has been populated
if ($where) { $where .= ' or'; } // Note space locations
foreach ($term as $wd) {
// The opening string alts should have caught
// this, but J.I.C. . . .
if (! empty($wd)) {
if ($andwhere) { $andwhere .= ' and'; }
$andwhere .= " $field like '%$wd%'";
}
}
// Encapsulate and's in (), long story but will give
// unexpected results if you don't. Again, note space.
if ($andwhere) { $anwhere = " ($andwhere)"; }
}
//
// You **could** just "select *", but I find this is good
// for specific field selections and efficiency if you
// don't need them all . . . create second array in that case.
//
$select = "select";
foreach ($fields as $field) { $select .= " $field,"; }
// Chop last , or change the above to only add , if it's
// not the last item in the selected fields array
$select = preg_replace('/,$/','',$select);
$select .= " from tablename";
//
// Finally, add the where, if it exists
if ($where) { $select .= " where $where"; }
// add order and limit after