Forum Moderators: coopster

Message Too Old, No Replies

Separating words and searching

         

kkonline

9:45 am on Aug 30, 2007 (gmt 0)

10+ Year Member



I have a table articles, in which i am storing tags (separated by commas)
example for an article tags are life,confidence,success . Now this all is stored as a string. I want to extract each tag and search the table for it's occurance that means next i want to search for life confidence and success in the table and display results.

Above is just an example the actual tags can be much longer and separated by comma

eelixduppy

2:50 pm on Aug 30, 2007 (gmt 0)



Looks like you are going to have to explode [php.net] the tags into their pieces and either write a query with multiple 'OR' conditions or loop through the tags and have multiple queries.

kkonline

3:47 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



Ok, that means i should use something like

<?php
$tags = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $tags);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2 and so on

But then how do i get to know how many tags are there that means $piece[5] exists but $piece[6] doesnot exist. How to know about this?

Can i use array_key_exists() to check if the tags exist in the array

and then i use a for($i=0;i<array key;i++) loop
{
SELECT DISTINCT id FROM wow WHERE 0 OR CONCAT(title,content,views,'') LIKE '%$piece[$i]%' ORDER BY id DESC;
}

Is the above logic correct? Please see the %$piece[$i]% query if it's structure/syntax is ok?

any suggestions or improvements?

d40sithui

5:30 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



to get the lenght of the array just use count($pieces) or sizeof($pieces) and put it into a for loop to get individual pieces
so something like this

<?
for($i = 0; $i<sizeof($pieces); $i++){
$query = "SELECT DISTINCT id FROM wow WHERE 0 OR CONCAT(title,content,views,'') LIKE '%".$pieces[$i]."%' ORDER BY id DESC";

}
?>

kkonline

5:15 am on Aug 31, 2007 (gmt 0)

10+ Year Member



SELECT DISTINCT id FROM wow WHERE 0 OR CONCAT(title,content,views,'') LIKE '%".$pieces[$i]."%' ORDER BY id DESC

The query above works fine, but sometimes if the pieces matches with the current article, it returns the id of the article i am viewing.

How to filter the current id of the article from search results?

current id is accessed by $page

d40sithui

11:33 am on Aug 31, 2007 (gmt 0)

10+ Year Member



try this.

SELECT DISTINCT id FROM wow WHERE 0 OR CONCAT(title,content,views,'') LIKE '%".$pieces[$i]."%' and id!=$page ORDER BY id DESC