データベースの記事を再分類したいと考えています。記事をより良く分類する良い考えがありません。参照テーブルを作成し、各カテゴリのすべての単語をその中に入れます。次に、メイン テーブルからクエリを実行し、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