Jump to content

Comparing MySql data and arrays.


kodie

Recommended Posts

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.

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.