exec(" CREATE TABLE IF NOT EXISTS countries ( id INT AUTO_INCREMENT PRIMARY KEY, name_en VARCHAR(255) NOT NULL, name_ar VARCHAR(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "); db()->exec(" CREATE TABLE IF NOT EXISTS cities ( id INT AUTO_INCREMENT PRIMARY KEY, country_id INT NOT NULL, name_en VARCHAR(255) NOT NULL, name_ar VARCHAR(255) DEFAULT NULL, UNIQUE KEY uniq_city_country (country_id, name_en), CONSTRAINT fk_cities_country FOREIGN KEY (country_id) REFERENCES countries(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "); if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_country'])) { $countryNameEn = trim($_POST['country_name_en'] ?? ''); $countryNameAr = trim($_POST['country_name_ar'] ?? ''); if ($countryNameEn === '') { $errors[] = 'Country name (English) is required.'; } else { try { $stmt = db()->prepare("INSERT INTO countries (name_en, name_ar) VALUES (?, ?)"); $stmt->execute([$countryNameEn, $countryNameAr !== '' ? $countryNameAr : null]); $flash = 'Country added.'; } catch (Throwable $e) { $errors[] = 'Country already exists or could not be saved.'; } } } elseif (isset($_POST['update_country'])) { $countryId = (int)($_POST['country_id'] ?? 0); $countryNameEn = trim($_POST['country_name_en'] ?? ''); $countryNameAr = trim($_POST['country_name_ar'] ?? ''); if ($countryId <= 0 || $countryNameEn === '') { $errors[] = 'Country ID and English name are required.'; } else { try { $stmt = db()->prepare("UPDATE countries SET name_en = ?, name_ar = ? WHERE id = ?"); $stmt->execute([$countryNameEn, $countryNameAr !== '' ? $countryNameAr : null, $countryId]); $flash = 'Country updated.'; $editCountryId = 0; } catch (Throwable $e) { $errors[] = 'Country could not be updated.'; } } } elseif (isset($_POST['delete_country'])) { $countryId = (int)($_POST['country_id'] ?? 0); if ($countryId <= 0) { $errors[] = 'Invalid country selected.'; } else { $stmt = db()->prepare("DELETE FROM countries WHERE id = ?"); $stmt->execute([$countryId]); $flash = 'Country deleted.'; $editCountryId = 0; } } } $countryNameExprNoAlias = $lang === 'ar' ? "COALESCE(NULLIF(name_ar, ''), name_en)" : "COALESCE(NULLIF(name_en, ''), name_ar)"; $countries = db()->query("SELECT id, name_en, name_ar, {$countryNameExprNoAlias} AS display_name FROM countries ORDER BY display_name ASC")->fetchAll(); $editingCountry = null; if ($editCountryId > 0) { foreach ($countries as $country) { if ((int)$country['id'] === $editCountryId) { $editingCountry = $country; break; } } } render_header('Manage Countries', 'admin', true); ?>

Countries

Manage the list of allowed countries for shipment routes.

Add country

Go to cities

Countries list

Cancel

No countries added yet.

ID Country (EN) Country (AR) Action