getMessage() . "\n"); } $resultsFile = __DIR__ . '/results.json'; if (!file_exists($resultsFile)) { die("No results file found.\n"); } $content = file_get_contents($resultsFile); $data = json_decode($content, true); if (empty($data) || !is_array($data)) { die("No data to process or invalid JSON.\n"); } $insertedCount = 0; $stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, start_location, end_location, price_amount, currency) VALUES (?, ?, ?, ?, ?, ?)"); foreach ($data as $row) { $taskId = $row['task_id'] ?? null; $appName = $row['app'] ?? 'Unknown'; $startLoc = $row['start_location'] ?? ''; $endLoc = $row['end_location'] ?? ''; $priceStr = $row['price'] ?? ''; // Extract numeric amount and currency from string like "2.5 JOD" $amount = 0.0; $currency = 'JOD'; // Match numeric parts (including decimals) and text parts if (preg_match('/([\d\.]+)\s*([A-Za-z]+|د\.ا)/', $priceStr, $matches)) { $amount = (float)$matches[1]; $currency = $matches[2]; if ($currency === 'د.ا') { $currency = 'JOD'; } } else { $amount = (float)$priceStr; } // Ignore invalid entries without an amount if ($amount <= 0) { continue; } if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) { $insertedCount++; } } // Clear the JSON file after successfully inserting data file_put_contents($resultsFile, json_encode([], JSON_PRETTY_PRINT)); echo "Successfully inserted $insertedCount records into the database and cleared results.json.\n";