2012-10-09 73 views
0

我必須保存到表中的數據的數量:限制到其他表

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

,但是,它仍然保存完整的數據。請幫幫我。謝謝:)

+0

'$ i <51'應該做什麼? –

+0

@lc。我想存儲最多50個current_sentence –

回答

3

在您的代碼中,您將$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循環出來。