我想让我的数据库文章重新分类。我没有好主意,把文章分类更好。我建立了一个参考表,把每个类别的每一个可能的词都放在里面。然后我从主表中查询,将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);
?>
但是速度非常非常慢,我在我的articles 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