kodie Posted November 30, 2009 Share Posted November 30, 2009 I have a mysql table set up like this: id subject body userid date time tags 1 blog subject blog body 1 11|29|2009 9|30|AM blog,site news,tech when displaying the data i want to be able to add "?id=blog&tags=blog,tech" to the url to search for multiple tags. i'm pretty close with this: if (isset($_GET['tags'])) { $tags = addslashes($_GET['tags']); $srchinfo = "WHERE tags IN ('$tags')"; }; $result = mysql_query("SELECT * FROM blogs $srchinfo ORDER BY id DESC LIMIT $offset, $rowsPerPage"); but, when using the url, it will only show up posts that have one of the tags. For example: Post1: site news, blog Post2: blog Post3: blog,site news "?id=blog&tags=blog" Only shows Post2 "?id=blog&tags=site news, blog" Only shows Post1 Anyone know what to do? Thank you. Quote Link to comment Share on other sites More sharing options...
trq Posted November 30, 2009 Share Posted November 30, 2009 $tags = explode(',', addslashes($_GET['tags'])); $srchinfo = "WHERE tags IN ('" . implode("','", $tags . "')"; Quote Link to comment Share on other sites More sharing options...
kodie Posted December 3, 2009 Author Share Posted December 3, 2009 $tags = explode(',', addslashes($_GET['tags'])); $srchinfo = "WHERE tags IN ('" . implode("','", $tags) . "')"; This doesnt work either because the "tags" data from the mysql table is still "blog,site news" and needs to be splitted somehow. Is there a way to do that? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 $tagsarray = explode(',', $_GET['tags']); $srchinfo= "WHERE tags IN ('" . implode("') AND tags IN ('", $tagsarray) . "')"; Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 This doesnt work either because the "tags" data from the mysql table is still "blog,site news" and needs to be splitted somehow. Why are you storing tags in a comma separated string? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 I was thinking the same, but at this point I'm guessing (s)he's already done quite a bit of coding on this. Really speaking there should be a tags table with the id to the blog post as a foreign key and a single row for each tag, the query those Quote Link to comment Share on other sites More sharing options...
kodie Posted December 3, 2009 Author Share Posted December 3, 2009 $tagsarray = explode(',', $_GET['tags']); $srchinfo= "WHERE tags IN ('" . implode("') AND tags IN ('", $tagsarray) . "')"; That doesnt work either. Still only shows results that have exactly what you searched for. i.e: "?id=blog&tags=test" shows items that only has the "test" tags and no other tag. "?id=blog&tags=test,news" shows items that have both the "test" and "news" tags in that order and no other tags. This doesnt work either because the "tags" data from the mysql table is still "blog,site news" and needs to be splitted somehow. Why are you storing tags in a comma separated string? I figured it was the easiest way. Guess not so much. lol. I was thinking the same, but at this point I'm guessing (s)he's already done quite a bit of coding on this. Really speaking there should be a tags table with the id to the blog post as a foreign key and a single row for each tag, the query those I don't quite understand, can you explain this a little more? or send me to a link that explains? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 How about $tagsarray = explode(',', $_GET['tags']); $srchinfo= "WHERE tags LIKE '%" . implode("%' AND tags LIKE '%", $tagsarray) . "%'"; Quote Link to comment Share on other sites More sharing options...
kodie Posted December 3, 2009 Author Share Posted December 3, 2009 How about $tagsarray = explode(',', $_GET['tags']); $srchinfo= "WHERE tags LIKE '%" . implode("%' AND tags LIKE '%", $tagsarray) . "%'"; JACK POT! thank you very much, that was the ticket. Quote Link to comment Share on other sites More sharing options...
kodie Posted December 3, 2009 Author Share Posted December 3, 2009 How about $tagsarray = explode(',', $_GET['tags']); $srchinfo= "WHERE tags LIKE '%" . implode("%' AND tags LIKE '%", $tagsarray) . "%'"; JACK POT! thank you very much, that was the ticket. Ok now i have another question if you don't mind helping me further: I'd like to be able to search for multiple tags using that method. i.e: "?id=blog&tags=test,site news" would give me results that have both of those tags regardless of what order they are in. Any ideas? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 The code I supplied should work regardless of the order Quote Link to comment Share on other sites More sharing options...
kodie Posted December 3, 2009 Author Share Posted December 3, 2009 The code I supplied should work regardless of the order oh! i was typing something wrong when testing it. Everything works perfectly as intended. Thanks again! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 No problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.