简体   繁体   English

限制保存到其他表的数据数量

[英]Limit the number of data that save to other table

I have to tables : 我要表:

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

I want to take the document data from tb_document . 我想从tb_document获取document数据。 the data is text and then parse the data become sentences. 数据是文本,然后将数据解析为句子。 I need to store maximum 50 sentences only for every document in tb_sentence. 我只需要为tb_sentence中的每个文档存储最多50个句子。

here's the code : 这是代码:

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++;
   }
}

but, it still save whole data. 但是,它仍然保存整个数据。 please help me. 请帮我。 thank you :) 谢谢 :)

In your code, you compare $i to 51, but don't do anything with the comparison. 在您的代码中,您将$i与51进行了比较,但是对比较不做任何事情。 Try: 尝试:

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; 
   }
}

The break statement will exit out of the foreach loop. break语句将退出foreach循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM