I want to let my database article reclassified. I explode the text of a given article, and then see if there is one word of the article match 1 tag which it has appear in the category table, then update this article in this category name. My code is here. I want make a limit that every category max has 5 articles. but the update limit not work. Thanks.
<?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("12",$db);
$result = mysql_query("SELECT title,content,id,cat,date FROM articles Order By date DESC"); //get all the articles
$count = 0;
$ids = array();
$categories = array('1','2','3','4','5','6','7','8','9','10');//category numbers, for 1 = art, 2 = travel... these are stored in another refrenced DB table
$curCategory = array_shift($categories);
echo $curCategory;
while ($row = mysql_fetch_array($result))
{
$tt = $row['title'].' '.$row['content'];
$tt = preg_replace('/[^a-zA-Z0-9 ]/','',$tt);
$words = preg_split("/\s+/",$tt);
$uniqueWords = array_keys(array_flip($words)); // broken article sentence into 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("12",$db);
mysql_query("SET NAMES utf8");
$query1 = mysql_query("SELECT count(*) as count FROM tag1 WHERE ($where) AND category ='count($categories)' "); //put the break words into reference table match out the category number
$count = 0;
while ($row = mysql_fetch_array($query1)) {
$count = $row['count'];
}
if($count) {
$ids[] = $row['id'];
$count++;
if($count == 5) {
mysql_query("UPDATE articles SET cat = '$curCategory' WHERE id in ('".implode("', '", $ids)."')"); //update every category max articles
if(!$curCategory = array_shift($categories)) {
break;
}
$count = 0;
$ids = array();
}
}
}
?>
reference table
category | tag1 | tag2 | tag3 | tag4 | tag5
1 | paint | picture| sculpture | photo | bronze
2 | tourism | travel | tour | journey | trip
3 | style | vogue | fashion | mode | Popular
... // 10 categories, category 1 = art , category 2 = travel ...