我必须表:
tb_document tb_sentence
================= ===========================================
|doc_id|document| | id | sentence_id |sentence | doc_id|
================= ===========================================
我想从tb_document
获取document
数据。数据是文本,然后将数据解析成句子。
我只需要为 tb_sentence 中的每个文档存储最多 50 个句子。
这是代码:
foreach ($content as $doc_id => $sentences){ //$content is variable that save document from tb_document
$i = 0;
foreach(preg_split('/\\.\\s*/', $sentences) as $current_sentence){
$q = mysql_query("INSERT INTO tb_sentence SET doc_id = {$doc_id}, sentence_id = {$i}, sentence = '{$current_sentence}'") or die(mysql_error());
$i<51; $i++;
}
}
但是,它仍然保存整个数据。请帮我。谢谢你:)
最佳答案
在您的代码中,您将 $i
与 51 进行比较,但不对比较执行任何操作。尝试:
foreach ($content as $doc_id => $sentences){ //$content is variable that save document from tb_document
$i = 0;
foreach(preg_split('/\\.\\s*/', $sentences) as $current_sentence){
$q = mysql_query("INSERT INTO tb_sentence SET doc_id = {$doc_id}, sentence_id = {$i}, sentence = '{$current_sentence}'") or die(mysql_error());
if(++$i >= 50)
break;
}
}
break
语句将从 foreach
循环中退出。
关于php - 限制保存到其他表的数据数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803324/