Ok folks. I have a Table categories with id, categoryname.
categories has: id=1 categoryname = batteries, id=2 categoryname = flashes, id=3 categoryname glasses.
The Second table is users. Users have id, user_eimal, my_choices.
Every user has store into my_choices the category name that wants to appear.
So for example user George with user_email xxxxx@xxx.com has store in my_choices: batteries,glasses
Now i want to bring up records from table products where categoryname is the values that user george has stored into my_choices.
Products has id, p_title, categoryname
for example i want to:
<?php
$usenmail = $_SESSION['SESS_EMAYL_NAME'];
$q1 = "SELECT * FROM categories WHERE id = '$id'";
$q2 = mysql_query($q1, $connectiondb) or die(mysql_error());
$results = mysql_fetch_assoc($q2);
$my_choices = $results['my_choices']; //That means $my_choices = batteries,glasses
//Now i want to select fom my table poducts only the categories (batteries,glasses)that user has store.
$trt = mysql_query ("select * from products where categoryname = batteries,glasses");
while($apst = mysql_fetch_assoc($trt)) {
echo '<div style="color: #000; float: left;">Productname: '.$['apst'].'</div>';
echo '<br>';
}
?>
You want to use a join table so you can have a many-to-many relationship. Your current structure is better suited to each user only having ONE choice.
What you want are three tables:
So to use your example, your three tables would look like this (if anyone knows a better way to demonstrate SQL tables on here, please let me know):
Users
Categories
Users_Categories
You would then use a select statement with a join clause to get George and his categories:
This would return two rows in the result:
Depending on what you want to do with that data, it may be better to also select the userID.
If you want a query that will only return one row with all of the information, it gets a little trickier. You have to use the GROUP BY functionality. That would look like this:
For your final question about products, you would follow the same logic with more joins. Your Products table wants to have a productID, product_name, and categoryID.