0

データベースの記事を再分類したいと考えています。記事をより良く分類する良い考えがありません。参照テーブルを作成し、各カテゴリのすべての単語をその中に入れます。次に、メイン テーブルからクエリを実行し、articlesすべての記事の内容を単語で調べて参照テーブルに配置し、記事がどのカテゴリに属しているかを照合します。その後、更新を行います。すべてのカテゴリの最大記事数を 5 に制限したいのですが、次のようなコードを書きます。

<?php
header('Content-type:text/html; charset=utf-8');
$db = mysql_connect("localhost","root","root") or die("can not connect Mysql Server");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT content,id,directory,date FROM articles WHERE Order By date DESC");//get all the articles from database
while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
$content = $row['content'];
$words = preg_split("/\s+/",$content); 
$uniqueWords = array_keys(array_flip($words)); 
$parts = '';
foreach($uniqueWords as $word){     
$parts[] = " tag1 = '$word' OR tag2 = '$word' OR tag3 = '$word' OR tag4 = '$word' OR tag5 = '$word' ";   
$where = implode(" OR ", $parts);
mysql_select_db("mydb",$db);
mysql_query("SET NAMES utf8");
$query = mysql_query("SELECT * FROM tags1 WHERE ($where) AND category='art' "); // put every word from each article into `tag1` database whether the article is match category='art'
$citn = '';
            while ($row = mysql_fetch_array($query)) {
        $citn = $row['category'];
        } 
    $catnew = $citn;
mysql_query("UPDATE articles SET directory = '".$catnew."' WHERE id = '".$id."' AND directory ='0'  Order By date DESC LIMIT 5"); // update first 5 articles which is match category='art' 
$query = mysql_query("SELECT * FROM tags1 WHERE ($where) AND category='fashion' "); // put every word from each article into `tag1` database whether the article is match category='fashion'
$citn = '';
            while ($row = mysql_fetch_array($query)) {
        $citn .= $row['category']."|" ;
        } 
    $catnew = explode("|",$citn);
mysql_query("UPDATE articles SET directory = '".$catnew."' WHERE id = '".$id."' AND directory ='0'  Order By date DESC LIMIT 5"); // update first 5 articles which is match category='fashion'
$query = mysql_query("SELECT * FROM tags1 WHERE ($where) AND category='travel' ");// put every word from each article into `tag1` database whether the article is match category='travel'
... // there have 20 `category`, each is the same judge method.
}
}
mysql_close($db);
?>

しかし、速度は非常に遅いです。記事のdbテーブルで60の記事をテストし、tag1テーブルで20のカテゴリをテストします。しかし、更新中にエコーします Fatal error: Maximum execution time of 60 seconds exceeded in...

効率的な更新の方法または、この作業を行うためのより良い方法はありますか? ありがとう。

これは私の記事テーブルです

id | title | link | content | date | directory
1  | ...   | ...  | ...     | ...  |  0 // all directory value are `0` 
2  | ...   | ...  | ...     | ...  |  0 
3  | ...   | ...  | ...     | ...  |  0
... // more 60 articles

これは私のtag1テーブルです

category | tag1    | tag2   | tag3       | tag4    |  tag5   
tourism  | tourism | travel | tour       | journey |  trip
fashion  | style   | vogue  | fashion    | mode    |  Popular
art      | paint   |
... // more 20 categories
4

1 に答える 1

0

申し訳ありませんが、あなたのコードを読んだとき、私を困惑させるあなたのコードに関する多くの質問があるので、私はあなたのコードを間違って読んだに違いないと感じています。

まず、次のような多くの領域があります。

  while ($row = mysql_fetch_array($query)) {
$citn = $row['category'];
} 

もちろん、実行すると $citn の最後のエントリのみが表示されますが、where 句でカテゴリを「art」に指定する必要があります。たとえば、結果として、クエリのポイントは何ですか? 「アート」になる?これが単純化されたコードでない限り。

だから、私が知る限り、あなたのコードが何をしていると思うか疑問に思っていました.'art'の最初のものは$ citnを'art'として返します.2番目のものは、それが見つけたtags1テーブル、それを配列に分解するため、複数のファッションがある場合、最後の更新はおそらく失敗しますそこにダンプされたPHP配列をSQLがどのように処理するかわかりません。コードを表示しない最後のクエリなので、少し迷っています。

特定の記事のテキストを分解して、5 つ以上のタグがカテゴリに表示されているかどうかを確認したいという印象を受けました。もしそうなら、私はこのコードがそれをしているのを見ません。

おっしゃる通り、どこか読み違えたのかもしれません。

于 2011-04-03T13:10:49.223 に答える