Each Article can have unlimited categories.
Categories are saved in the database like this
example article_category: '1','3','8'
OR
example article_category: '2','3'
So when I search for a listing under, lets say category 3
$category_id = $_REQUEST['category_id']; //3
$SQL = "SELECT * FROM ARTICLES WHERE article_category = '$category_id'";
If it were just one the above would work fine.
I hate to say it but I'm completely lost.. Would I use IN ?
You can use
FIND_IN_SET
here, but it may not be very efficient. I suggest that you change your table structure and follow @jdias' answer.If your
article_category
is saved as1,2,3
:Or if it's saved as
'1','2','3'
:The way I would implement this is by creating a table for categories, which you probably already have. Something like:
Then create a table to link articles to categories. Something like:
The above table basically replaces the field article_category you have currently. In the above example
And when you need to get all the articles for a given category, all you would run a query like:
You could even do a join with articles database to output the article titles if you wish.
I hope this helps. Good luck!
= (equals) check against the complete string. what you want to do could be done using LIKE and the % comodin (it's like * for files in
dir
or the like)however, i reccommend that you normalize the database and have categories as a subtable