Hello!
I have some prob. whit a function that search a table whit shop items.
The table:
id | groupId | name |
1 | 1 | itm1 |
2 | 1 | itm2 |
3 | 2 | itm3 |
4 | 1 | itm4 |
If groupId == 0 (func. arg) I want to search all the goups by name,
if groupId > 0 I want to search just the selected group.
if i groupId == 0 it gives a correct result irrespective of the seach string
if i groupId == 1 and the search string == 'itm2 itm3' it gives a correct result
The prob. is:
if i groupId == 1 and the search string == 'itm3 itm2'(oposit) it ignores the groupId and gives all that match the string.
function seek($groupId = 0, $seekStr = "") {
$loop = '';
$arr = explode(' ', $seekStr);
foreach($arr as $key => $elem )
if($key == 0 )
$loop.= " '%$elem%' ";
else
$loop.= " OR name LIKE '%$elem%' ";
$sql = "SELECT * FROM items WHERE name LIKE $loop";
if($groupId > 0)
$sql.= " AND groupId = $groupId";
return querAndFetch($sql);
}
I`m sorry for my bad English, but hope someone understand my prob.