tb_content (left) and tb_word (right):
===================================== ================================ |id|sentence |sentence_id|content_id| |id|word|sentence_id|content_id| ===================================== ================================ | 1|sentence1| 0 | 1 | | 1| a | 0 | 1 | | 2|sentence2| 1 | 1 | | 2| b | 0 | 1 | | 3|sentence5| 0 | 2 | | 3| c | 1 | 1 | | 4|sentence6| 1 | 2 | | 4| a | 1 | 1 | | 5|sentence7| 2 | 2 | | 5| e | 1 | 1 | ===================================== | 6| f | 0 | 2 | | 7| g | 1 | 2 | | 8| h | 1 | 2 | | 9| i | 1 | 2 | |10| f | 2 | 2 | |11| h | 2 | 2 | |12| f | 2 | 2 | ================================
I need to check if each sentence consists of words belonging to other sentences in each content_id .
eg:
Check content_id = 1 that they are sentence1 and sentence2 . from tb_word , we can see that sentence1 and sentence2 consist of the same word a . if the number a in two sentences >=2 , then a will be the result. Therefore, if I print the result, it should be: 00Array ( [0] => a [1] => b) 01Array ( [3] => a ) 10Array ( [3] => a )11Array ( [0] => c [1] => a [2] => e) where 00 means sentence_id = 0 and sentence_id = 0
firstly, I do a functionTotal to count the amount of sentence belonging to each content_id :
$total = array(); $sql = mysql_query('select content_id, count(*) as RowAmount from tb_content Group By contente_id') or die(mysql_error()); while ($row = mysql_fetch_array($sql)) { $total[] = $row['RowAmount']; } return $total;
From this function I get the value $total and from this I need to check the similarity of some words (from tb_word ) between all the capabilities of 2 sentence
foreach ($total as $content_id => $totals){ for ($x=0; $x <= ($totals-1); $x++) { for ($y=0; $y <= ($totals-1); $y++) { $shared = getShared($x, $y); } }
getShared function is equal to:
function getShared ($x, $y){ $token = array(); $shared = array(); $i = 0; if ($x == $y) { $query = mysql_query("SELECT word FROM `tb_word` WHERE sentence_id ='$x' "); while ($row = mysql_fetch_array($query)) { $shared[$i] = $row['word']; $i++; } } else { $query = mysql_query("SELECT word, count(word) as jml FROM `tb_word` WHERE sentence_id ='$x' OR sentence_id ='$y' GROUP BY word "); while ($row = mysql_fetch_array($query)) { $jml = $row['jml']; $token[$i] = $row['word']; if ($jml >= 2) { $shared[$i] = $token[$i]; } $i++; }
But the result that I get is still erroneous. the result is still mixed between different content_id . the result should also be a content_id group. sorry for my bad english and my bad explanation. cmiiw, please help me .. thanks :)