Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-matkort-handler/
.github/
add_logs.php
admin/
api/
card_balance.php
classes/
config/
food_logs.php
forgot_password.php
includes/
index.php
insert_restaurants.php
install.php
login.php
logout.php
public/
register.php
reset_password.php
verify.php
insert_restaurants.php
47 lines ASCII Windows (CRLF)
<?php
session_start();
require_once './config/database.php';
require_once './classes/Restaurant.php';
$restaurantModel = new Restaurant($pdo);
$jsonPath = __DIR__ . '/public/data/restaurants.json';
$json = file_get_contents($jsonPath);
if ($json == false) {
http_response_code(500);
exit('Could not read JSON file: ' . $jsonPath);
}
$restaurants = json_decode($json, true);
if(json_last_error() !== JSON_ERROR_NONE) {
http_response_code(500);
exit('Invalid JSON: ' . json_last_error_msg());
}
if (!is_array($restaurants)) {
http_response_code(500);
exit('JSON must decode to an array');
}
foreach($restaurants as $restaurantRow) {
if (!is_array($restaurantRow)) {
continue;
}
$name = $restaurantRow['name'] ?? null;
$location = $restaurantRow['address'] ?? null;
$lat = isset($restaurantRow['lat']) ? (float)$restaurantRow['lat']: null;
$lng = isset($restaurantRow['lng']) ? (float)$restaurantRow['lng'] : null;
if (!$name || !$location || !$lat || !$lng) {
echo "Skippade restaurang";
continue;
}
try {
$restaurantModel->create($name, $location, $lat, $lng);
echo "Skapade restaurang: " . $name . "<br>";
} catch (Exception $e) {
echo $e->getMessage();
}
}