Update: 2026-06-30 20:54:00
This commit is contained in:
@@ -38,16 +38,34 @@ $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'] ?? '';
|
||||
if (isset($row['status']) && $row['status'] !== 'success') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$taskId = $row['task_id'] ?? null;
|
||||
$resultData = $row['result_data'] ?? [];
|
||||
|
||||
$appName = $resultData['app'] ?? $row['app'] ?? 'Unknown';
|
||||
|
||||
$startLoc = $resultData['start_location'] ?? $row['start_location'] ?? '';
|
||||
if (empty($startLoc) && !empty($resultData['start_lat'])) {
|
||||
$startLoc = "Lat: {$resultData['start_lat']}, Lng: {$resultData['start_lng']}";
|
||||
}
|
||||
|
||||
$endLoc = $resultData['end_location'] ?? $row['end_location'] ?? '';
|
||||
if (empty($endLoc) && !empty($resultData['end_lat'])) {
|
||||
$endLoc = "Lat: {$resultData['end_lat']}, Lng: {$resultData['end_lng']}";
|
||||
}
|
||||
|
||||
$priceRaw = $resultData['price'] ?? $row['price'] ?? 0;
|
||||
|
||||
// Extract numeric amount and currency from string like "2.5 JOD"
|
||||
$amount = 0.0;
|
||||
$currency = 'JOD';
|
||||
|
||||
if (is_numeric($priceRaw)) {
|
||||
$amount = (float)$priceRaw;
|
||||
} else {
|
||||
$priceStr = (string)$priceRaw;
|
||||
// Match numeric parts (including decimals) and text parts
|
||||
if (preg_match('/([\d\.]+)\s*([A-Za-z]+|د\.ا)/', $priceStr, $matches)) {
|
||||
$amount = (float)$matches[1];
|
||||
@@ -58,6 +76,7 @@ foreach ($data as $row) {
|
||||
} else {
|
||||
$amount = (float)$priceStr;
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore invalid entries without an amount
|
||||
if ($amount <= 0) {
|
||||
@@ -66,6 +85,8 @@ foreach ($data as $row) {
|
||||
|
||||
if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) {
|
||||
$insertedCount++;
|
||||
} else {
|
||||
echo "Failed to insert task_id: $taskId. Error: " . implode(" ", $stmt->errorInfo()) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user