1) { $block_name = trim($arr[2]); $room_type_name = trim($arr[3]); $data = [ $year.'-01-01' => round(str_replace(',', '.',$arr[4])), $year.'-02-01' => round(str_replace(',', '.',$arr[5])), $year.'-03-01' => round(str_replace(',', '.',$arr[6])), $year.'-04-01' => round(str_replace(',', '.',$arr[7])), $year.'-05-01' => round(str_replace(',', '.',$arr[8])), $year.'-06-01' => round(str_replace(',', '.',$arr[9])), $year.'-07-01' => round(str_replace(',', '.',$arr[10])), $year.'-08-01' => round(str_replace(',', '.',$arr[11])), $year.'-09-01' => round(str_replace(',', '.',$arr[12])), $year.'-10-01' => round(str_replace(',', '.',$arr[13])), $year.'-11-01' => round(str_replace(',', '.',$arr[14])), $year.'-12-01' => round(str_replace(',', '.',$arr[15])), ]; $sql = "SELECT * FROM blocks WHERE name='".$block_name."' LIMIT 1"; $res = mysql_query($sql); while ($obj = mysql_fetch_assoc($res)) { $block_id = $obj['id']; } if (!$block_id) continue; $sql = "SELECT * FROM room_types WHERE name='".$room_type_name."' LIMIT 1"; $res = mysql_query($sql); while ($obj = mysql_fetch_assoc($res)) { $room_type_id = $obj['id']; } if (!$room_type_id) continue; foreach ($data as $month => $item) { if ($item > 0) { $data_sales[$block_id][$room_type_id][$month][] = $item; } } } } fclose($file); foreach ($data_sales as $block_id => $block_data) { foreach ($block_data as $room_type_id => $dates) { foreach ($dates as $date => $prices) { $count = count($prices); $sum = 0; foreach ($prices as $price_item) { $sum += $price_item; } $price = round($sum / $count); $sql = "SELECT id, price, quantity FROM nb_sales WHERE block_id='".$block_id."' AND room_type_id='".$room_type_id."' AND period='".$date."' LIMIT 1"; if($rez = mysql_query($sql)) { if(empty($obj = mysql_fetch_assoc($rez))) { $sql = "INSERT INTO nb_sales (block_id, room_type_id, price, period) VALUES ('" . $block_id . "', '" . $room_type_id . "', '" . $price . "', '" . $date . "')"; $err = "insert"; } else { $sql = "UPDATE nb_sales SET price='" . $price . "' WHERE id='" . $obj['id']."'"; $err = "update"; } if(!mysql_query($sql)) echo "ERROR: Could not able to execute $sql. " . mysqli_error(); else echo "Records ".$err." successfully.
"; } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error(); } } } }