multi-array search query with delimited field
Pregunta
I have a array which store some country names
im doing a search module
and i wanna to find the db records whether contain the country names or not
but since db design problem , those record can own mutil counties and store the countries by delimiter "|"
below is a example
input: array("Cyprus","Austria") // note that the max input will be 300 country
db record country column: Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina|Bulgaria|Croatia|Cyprus|Czech Republic|Denmark|Estonia|European Union|Faroe Islands
So anyone can give a solution base on php for seaching?
Solución
First of all your DB design is not good. You should not be putting multiple delimited country names in one filed. You might wanna change it.
If you really want to work with the existing design, you can make use of explode
and in_array
functions of PHP as:
$db_rec = 'Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina';
$arr = explode('|',$db_rec);
$key = 'Albania';
if(in_array($key,$arr)) {
echo "$key found";
}