
Well, I've just been reading the manual http://www.mysql.com/doc/S/t/String_functions.html and discovered the function FIND_IN_SET() which is exactly what you want!
Code:
FIND_IN_SET(str,strlist)
Returns a value 1 to N if the string str is in the list strlist consisting of N
substrings. A string list is a string composed of substrings separated by `,'
characters. If the first argument is a constant string and the second is a column of
type SET, the FIND_IN_SET() function is optimized to use bit arithmetic! Returns 0
if str is not in strlist or if strlist is the empty string. Returns NULL if either
argument is NULL. This function will not work properly if the first argument
contains a `,':
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2
So something like this might be handy,
SELECT id, text, rubrieken, FIND_IN_SET('$rub', rubrieken) AS position
FROM pages
WHERE FIND_IN_SET('$rub', rubrieken)
Now you will have a result set with records that contain that rubrieken. You also have the position in the set where the rubrieken appears - which if that is useful to you.
Eg, say the value of the rubrieken field is "10,11,12" and you are searching for "12" then position will have the value 3 because "12" was found in the third element of the string (as a comma delimited set).
Lets say you have extracted the row from the result set as an associateive array, you could do this:
$row = mysql_fetch_array($result)
$rubriekens = explode(",", $row["ruberiek"]); // put rubrieks into an array
You now know that the relevent rubrieken (12) occupies $row["position"] - 1 element of the array $rubriekens. So, for example you could over-ride that value if you wanted to (say change it from 12 to 13) like so:
$i = $row["position"] - 1;
$rubriekens[$i] = 13;
Make sense ?!? Anyway, I'm just thinking out aloud.
Bookmarks