I have a field in my database that is an array ie: id_ingredients = "1,8,3,9,5,2,7,4"
I want to compare a variable that is also an array to give a result if any of the variable ids exist in the field ids.
I am trying to check to see if a product's ingredients contain any of the ingredients in the variable. If someone is allergic to nuts for example i want to selet all products that contain nuts of any kind. So i query my ingredients to get the id of any ingredient that has the word "nut" in its name. Then i need to get the products with any of the ingredient ids.
This is what I have...
$alg = $_POST['alg'];
mysql_select_db($database, $Products);
$query_availIngredients = "SELECT * FROM ingredients WHERE ingredient LIKE '%$alg%' ";
$availIngredients = mysql_query($query_availIngredients, $Products) or die(mysql_error());
$row_availIngredients = mysql_fetch_assoc($availIngredients);
$ingarray = array();
do{
$ingarray[] = $row_availIngredients['id'];
} while ($row_availIngredients = mysql_fetch_assoc($availIngredients));
$alg = implode (',', $ingarray);
mysql_select_db($database, $Products);
$query_Products = "SELECT *FROM products WHERE
id_ingredients LIKE '%$alg%' " ;
$Products = mysql_query($query_Products, $Products) or die(mysql_error());
$row_Products = mysql_fetch_assoc($Products);
Thank you in advance for your help.
Placing an array of IDs in a single database field isn't really a good idea and you lose some of the power a relational database gives you by designing your database this way.
Rather than storing an array of ingredient IDs in your product's table I would create a third table.
An example table schema would be like this:
Products id productName productDescription
Ingredients id ingredientName
ProductIngredients id id_Products id_Ingredients
Some sample data might look like:
Products id productName productDescription 1 Peanutbutter And Jelly Sandwich Best sandwich ever
Ingredients id ingredientName 1 Peanutbutter 2 Jelly 3 Special sauce 4 Bread
ProductIngredients id id_Products id_Ingredients 1 1 1 2 1 2 3 1 4
You could then get a list of IDs of ingredients containing the word 'nut' like this:
SELECT id FROM Ingredients WHERE ingredientName LIKE '%nut%'
And a list of products containing ingredients which contain the word 'nuts' like this:
SELECT
Products.productName
FROM
Products
LEFT JOIN ProductIngredients ON ProductIngredients.id_Products = Products.id
LEFT JOIN Ingredients ON Ingredients.id = ProductIngredients.id_Ingredients
WHERE
Ingredients.ingredientName LIKE '%nut%'
You could get a list of ingredients for a product like this:
SELECT
Ingredients.ingredientName
FROM
Ingredients
LEFT JOIN ProductIngredients ON ProductIngredients.id_Ingredients = Ingredients.id
WHERE
ProductIngredients.id_Products = 1
This would give you a list such as this:
This is is called a many-to-many relationship.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.