getMessage() . "\n"); } echo "Fetching ALL rows to recalculate distance and duration...\n"; // Get all valid rows $stmt = $con->prepare(" SELECT id, start_lat, start_lng, end_lat, end_lng, country_code, price_amount FROM scraped_competitor_prices WHERE start_lat IS NOT NULL AND start_lng IS NOT NULL "); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$rows || count($rows) === 0) { echo "No rows found.\n"; exit(0); } echo "Found " . count($rows) . " rows to update. Processing...\n"; $updateStmt = $con->prepare(" UPDATE scraped_competitor_prices SET duration_min = :duration_min, distance_km = :distance_km, price_per_km = :price_per_km WHERE id = :id "); $successCount = 0; $failCount = 0; foreach ($rows as $index => $row) { $id = $row['id']; $countryCode = $row['country_code'] ?? 'JO'; $price = (float)$row['price_amount']; // Call Intaleq Maps (OSRM API) $routeInfo = getOsrmRouteDetails($row['start_lat'], $row['start_lng'], $row['end_lat'], $row['end_lng'], $countryCode); if ($routeInfo && isset($routeInfo['duration_min']) && isset($routeInfo['distance_km'])) { $dist = (float)$routeInfo['distance_km']; $dur = (int)round($routeInfo['duration_min']); $pricePerKm = $dist > 0 ? ($price / $dist) : 0; $updateStmt->execute([ ':duration_min' => $dur, ':distance_km' => $dist, ':price_per_km' => $pricePerKm, ':id' => $id ]); $successCount++; echo "Row $id: Distance {$dist}km, Duration {$dur}min, Price/Km {$pricePerKm} [SUCCESS]\n"; } else { $failCount++; echo "Row $id: Failed to fetch route details.\n"; } // Small sleep to avoid hitting API rate limits too hard (100ms) usleep(100000); } echo "======================================\n"; echo "Completed processing " . count($rows) . " rows.\n"; echo "Success: $successCount\n"; echo "Failed: $failCount\n";