-2
我正在一個電子商務網站上工作。php運行的過程將停止
當我運行產品(這些產品的詳細信息存儲在一個文件中),他們插入數據庫,我會通過手動停止該過程。最後一個插入ID被保存,這個ID來自產品細節而不是數據庫插入的ID。
我的查詢是,當我將重新運行該過程時,產品從最後一個插入產品ID獲得,但是它從文件的開始處開始。我的需要是從停止的地方開始。
我正在一個電子商務網站上工作。php運行的過程將停止
當我運行產品(這些產品的詳細信息存儲在一個文件中),他們插入數據庫,我會通過手動停止該過程。最後一個插入ID被保存,這個ID來自產品細節而不是數據庫插入的ID。
我的查詢是,當我將重新運行該過程時,產品從最後一個插入產品ID獲得,但是它從文件的開始處開始。我的需要是從停止的地方開始。
選項1
插入元素逐一表所示:
// take all data
$prods = json_decode(file_get_contents('products.php'));
while (!empty($prods)) {
// remove first element from array. Origin array is changed
$product = array_shift($prods);
// do your insert logic
$this->insert($product);
// for some reasons you don't insert all products
if ($this->break) {
break;
}
}
// write to file what is left, if any
file_put_contents('products.php', json_encode($prods));
選項2
產品排序ID和空循環,當你沒有達到您的元素:
foreach ($products as $product) {
if ($product->id < $lastId) {
continue;
}
$this->insert($product);
}
感謝您的回覆,但它不適合我。原因是選項1 ==>產品總數超過11 lac,所以如果我存儲的話,時間會過長。 Option2 ==>產品ID是隨機的。 –
沒有代碼 - 沒有幫助 – Justinas
其實這是一個常見的概念,我也不知道我會給出哪些代碼,因爲有那麼多的代碼行。 –