rosen_
rosen_

Reputation: 248

count the same data of words with data in a table

I have sentence, I wanna split into words, and then check them with the data in stopword table. I wanna count the number of same data (total). but, total doesn't give me the total of same data. How to sum the data I need ? thanks

$word ='temporal. the text mining in a evolutionary a theme patterns  theme threads  clustering'; 
$symbol    = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
$cleanMeta = str_replace($symbol, " ", $word);
$key     = strtolower($cleanMeta);
$key = explode(" ", trim($key));

foreach($key as $word_key){
    $query = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE stoplist_word = '$word_key'");
    while ($row = mysql_fetch_array($query)) {
        $row1 = $row['total'];
        echo $row1;
    }
}

Upvotes: 0

Views: 110

Answers (1)

Hugo Delsing
Hugo Delsing

Reputation: 14173

considering you already cleaned the input string, you dont need a foreach with a new query for every word. You could do:

$word ='temporal. the text mining in a evolutionary a theme patterns  theme threads  clustering'; 
$symbol    = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
$cleanMeta = str_replace($symbol, " ", $word);
$key       = trim(strtolower($cleanMeta));
$key       = str_replace("'","''",$key);
$keys      = "'".str_replace(" ","', '", $key)."'";

$query  = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE   stoplist_word IN ($keys)");
$row    = mysql_fetch_array($query);
$total = $row['total'];

echo $total;

if you realy want the foreach use:

$total = 0;
foreach($key as $word_key){
    $query = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE stoplist_word = '$word_key'");
    while ($row = mysql_fetch_array($query)) {
        $total += $row['total'];
    }
}
echo $total;

Upvotes: 1

Related Questions