Files

77 lines
1.9 KiB
PHP

<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
// ==== DB CONFIG ====
const DB_DSN = 'mysql:host=serwer1852487.home.pl;port=3380;dbname=28477142_0000006;charset=utf8mb4';
const DB_USER = '28477142_0000006';
const DB_PASS = '2aodCAiQSKThx4la6';
try {
$pdo = new PDO(DB_DSN, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode(['error' => 'DB error']);
exit;
}
// === GET ALL PLACES ===
$places = $pdo->query("
SELECT id, name, woj
FROM salon_places
ORDER BY woj ASC, name ASC
")->fetchAll();
$result = [];
foreach ($places as $place) {
// === GET SHOPS FOR PLACE ===
$stmt = $pdo->prepare("
SELECT address, open_hours, url_address, url_shop, lat, lng
FROM shops
WHERE place_id = ?
ORDER BY address ASC
");
$stmt->execute([$place['id']]);
$shops = [];
foreach ($stmt as $shop) {
$shopItem = [
'lat' => (string)$shop['lat'],
'lng' => (string)$shop['lng'],
];
if (!empty($shop['address'])) {
$shopItem['address'] = $shop['address'];
}
if (!empty($shop['open_hours'])) {
$shopItem['openHours'] = $shop['open_hours'];
}
if (!empty($shop['url_address'])) {
$shopItem['urlAddress'] = $shop['url_address'];
}
if (!empty($shop['url_shop'])) {
$shopItem['shopUrl'] = $shop['url_shop'];
}
$shops[] = $shopItem;
}
$result[] = [
'type' => "city", //! do sprawdzenia czy city jest potrzebne
'name' => $place['name'],
'woj' => $place['woj'] === null ? false : $place['woj'],
'shops' => $shops
];
}
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;