0

我必须表:

tb_document           tb_sentence
=================     ===========================================
|doc_id|document|     | id | sentence_id |sentence | doc_id|
=================     ===========================================

我想从中获取document数据tb_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++;
   }
}

但是,它仍然保存整个数据。请帮我。谢谢你 :)

4

1 回答 1

3

在您的代码中,您与$i51 进行比较,但对比较不做任何事情。尝试:

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循环。

于 2012-10-09T15:29:44.297 回答