2012-03-02 21 views
0

出力されたときにフィルムレビューを出力するシステムがあります。これを行うには、負と正の2つのテーブルから表示されるキーワードが必要ですか?コードは次のとおりですキーワードが強調表示されたPHPを出力するPHP

<?php 

// Connect to database 
mysql_connect("sdd", "sdsd", "") or die(mysql_error()); 
mysql_select_db("sdsd") or die(mysql_error()); 

$id = mysql_real_escape_string($_POST['reviewid']); 

//$query = "select * from review where id = '$id'"; 
$query = mysql_fetch_assoc(mysql_query("SELECT filmreview FROM review WHERE id = '$id'")); 
$pos = mysql_query("SELECT word FROM positive"); 
$neg = mysql_query("SELECT word FROM negative"); 


//Variables 
$review_text = $query['filmreview']; 
$good = 0; 
$bad = 0; 


// Gets words in to a text array and converts to lower case 
$cnt_r = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1))); 

// Gets the positive words and check for the word in the text 
    while($check = mysql_fetch_assoc($pos)){ 
     $lower = mb_strtolower($check['word']); 
    if(isset($cnt_r[$lower])){ 
    $good+= $cnt_r[$lower]; 
    echo $check ['word']; 
    echo "<p>"; 
    } 
    } 

// Gets the negative words and check for the word in the text 
while($check = mysql_fetch_assoc($neg)){ 
    $lower = mb_strtolower($check['word']); 
if(isset($cnt_r[$lower])){ 
    $bad+= $cnt_r[$lower]; 
     echo $check ['word']; 
     echo "<p>"; 
} 
} 


// If there are more positive words than negative than the review is positive 
if ($good > $bad) 
{ 
    echo "<p>"; 
    echo "This is a positive review"; 
    echo "<p>"; 
} 

// If there are more negative words than positive than the review is negative 
else if ($good < $bad) 
{ 
    echo "<p>"; 
    echo "This is a negative review"; 
    echo "<p>"; 
} 

// If there are the same amount of positive and negative words than the review is average 
else if ($good == $bad) 
{ 
    echo "<p>"; 
    echo "This is an average review"; 
    echo "<p>"; 
} 

//Prints out the number of postive and negative words found 
echo "Good words: " . $good . " and Bad words: " . $bad; 
echo "<p>"; 
echo $query ['filmreview']; 
echo "<p>"; 
echo "This is <font color=\"blue\">blue</font>!"; 



echo "<form method='post' action='welcome.html'>"; 
echo "<input type='submit' name='searchagain' value='Search'>"; 
?> 
+0

返信ありがとうございますが、私は仕事をすることができません。何が間違っているのか分かりませんが、 – user1045566

答えて

0

だけで検索すると、テキスト内のキーワードを検索し、ちょうどそれらを交換してください。 だから大体

if($postivekeyword) { 
     $newpostivekeyword = '<span class="positive">'.$postivekeyword.'</span>'; 
    } 
    if($negativekeyword) { 
     $newnegativekeyword = '<span class="negative">'.$negativekeyword.'</span>'; 
    } 

それからちょうど

$new_review_text = str_replace($postivekeyword, $newpostivekeyword, $review_text); 
+0

これに色を含めるにはどうすればいいですか?何も出力されないので – user1045566

+0

あなたのCSSにクラスを追加するだけです .negative {color:red; } 。ポジティブ{カラー:ブラック; } もちろん、ファイルにはどこかで必要です echo $ new_review_text; –

+0

何らかの理由でこれが動作していないようです – user1045566

2

CSSクラスを使用できます。あなたのスタイルシートにしたり、CSSクラスを作成することができますページ内のスタイルタグで

p .negative { color: red; } 
p .positive { color: black; } 

は今ちょうどあなたの段落にクラスを追加し、それはあなたが望むスタイルを適用します。

// If there are more positive words than negative than the review is positive 
if ($good > $bad) 
{ 
    echo '<p class="positive">'; 
    echo 'This is a positive review'; 
    echo '</p>'; 
} 

// If there are more negative words than positive than the review is negative 
else if ($good < $bad) 
{ 
    echo '<p class="negative">'; 
    echo 'This is a negative review'; 
    echo '</p>'; 
} 

など.....

+0

がありますが、テキスト内の言葉をレビュータイプではなく色付けしたいのですが。 – user1045566

+0

あなたは同じ価格を使うことができます。段落全体のクラスではなく、各単語の最初と最後に追加するスパンクラスを使用してください。 – chapman84

-1
  1. は、あなたがそれを送った直後に、クエリの応答を読み取ろう。あなたが複数のクエリの結果は、あなたのメモリ
  2. $cnt_r = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1)));を詰まらせるのを避けるため、この方法は$cnt_r = str_word_count(mb_strtolower($review_text), 2);
  3. str_word_countのアップfrakedバージョンであるように思わマルチバイトは安全ではありません。 Smarty私はpreg_match_all('#[\w\pL]+#u', $review_text, $matches);をstr_word_count()のUTF-8バージョンのベースとして使用しました。
  4. 近い
  5. は、いくつかの識別名
  6. あなたの変数を与える見て、常に正しく出力をesacape PDOを与える - )はhtmlspecialchars(使用するためにそこにあります!
  7. 2012年です - <font>タグは使用していません。代わりにCSSを使用してください!
  8. <?php 
    $query = mysql_fetch_assoc(mysql_query("SELECT filmreview FROM review WHERE id = '$id'")); 
    $review_text = $query['filmreview']; 
    
    // Gets words in to a text array and converts to lower case 
    $review_words = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1))); 
    
    $positive_words = array(); 
    $negative_words = array(); 
    $positive_words_count = 0; 
    $negative_words_count = 0; 
    
    // Gets the positive words and check for the word in the text 
    $res = mysql_query("SELECT word FROM positive"); 
    while ($row = mysql_fetch_assoc($res)){ 
        // why isn't this already stored lower-case in database? 
        $_word = mb_strtolower($row['word']); 
        if (isset($review_words[$_word])){ 
         $positive_words_count += $review_words[$_word]; 
         $positive_words[] = $_word; 
        } 
    } 
    
    // Gets the negative words and check for the word in the text 
    $res = mysql_query("SELECT word FROM negative"); 
    while ($row = mysql_fetch_assoc($res)){ 
        // why isn't this already stored lower-case in database? 
        $_word = mb_strtolower($row['word']); 
        if (isset($review_words[$_word])){ 
         $negative_words_count += $review_words[$_word]; 
         $negative_words[] = $_word; 
        } 
    } 
    
    if ($positive_words_count > $negative_words_count) { 
        // If there are more positive words than negative than the review is positive 
        echo "<p>This is a positive review<p>"; 
    } elseif ($positive_words_count < $negative_words_count) { 
        // If there are more negative words than positive than the review is negative 
        echo "<p>This is a negative review<p>"; 
    } else { 
        // If there are the same amount of positive and negative words than the review is average 
        echo "<p>This is an average review<p>"; 
    } 
    
    // highlight positive/negative words 
    $review_text = htmlspecialchars($review_text); 
    $pattern = '#\b(' . join('|', $positive_words) . ')\b#i'; 
    $review_text = preg_replace($pattern, "<span class=\"positive\"\\1</span>", $review_text); 
    $pattern = '#\b(' . join('|', $negative_words) . ')\b#i'; 
    $review_text = preg_replace($pattern, "<span class=\"negative\"\\1</span>", $review_text); 
    
    //Prints out the number of postive and negative words found 
    echo "Good words: " . $positive_words_count . " and Bad words: " . $negative_words_count; 
    echo "<p>" . $review_text . "<p>"; 
    echo "This is <font color=\"blue\">blue</font>!"; 
    

    とどこかCSSでspan.negativespan.positiveがどのように見えるかを定義


+0

どのようにCSSを定義しましたか?前に使ったことはありません – user1045566

+0

goole for " "あなたはhttp://www.w3.org/MarkUp/Guide/Styleのような有用なものを見つけるでしょう – rodneyrehm

+0

doesntはうまくいくようです – user1045566

関連する問題