Contenuto del File
```json
{
"root": "Museo del 900",
"url": "https://www.m
output in italiano e in formato json: root=sito, [0-x] per ogni evento, e i campi sotto indicati. Aggiungi anche informazioni che hai tu solo se sei moltissimo sicuro che siano pertinenti e corrette
Questo e' un esempio di file json seguito dallo schema del database mysql. Sulla base di questo scrivi il codice php con il quale importare i dati leggendo tanti file .json tutti nella stessa cartella.
```json
{
"root": "Museo del 900",
"url": "https://www.museodelnovecento.org/",
"events": [
{
"name": "Presentazione “Pablo Echaurren. Art for the many”",
"startdate": "01 ott 2025",
"enddate": "01 ott 2025",
"price": 0,
"location": {
"name": "FORUM900",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "10:00 - 19:30",
"url": "/web/museo-del-900/-/presentazione-pablo-echaurren.-art-for-the-many-",
"other": "Evento gratuito"
},
{
"name": "Visita guidata gratuita primo giovedì del mese | ore 19.30",
"startdate": "02 ott 2025",
"enddate": "02 ott 2025",
"price": 0,
"location": {
"name": "EDU900 | ore 19.30",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "19:30",
"url": "/web/museo-del-900/-/visita-guidata-gratuita-primo-giovedi-del-mese-ore-19.30-2-ottobre-2025",
"other": "Evento gratuito"
},
{
"name": "Giornata del Contemporaneo 2025",
"startdate": "04 ott 2025",
"enddate": "04 ott 2025",
"price": 0,
"location": {
"name": "EDU900",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "10:00 - 19:30",
"url": "/web/museo-del-900/-/giornata-del-contemporaneo-2025",
"other": "Evento gratuito"
}
]
}
```
CREATE TABLE events (
id INT AUTO_INCREMENT PRIMARY KEY,
root VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
startdate DATE NOT NULL,
enddate DATE NOT NULL,
price DECIMAL(10, 2) NOT NULL,
location_name VARCHAR(255) NOT NULL,
location_city VARCHAR(255) NOT NULL,
location_address VARCHAR(255) NOT NULL,
transport VARCHAR(255) NOT NULL,
hours VARCHAR(50) NOT NULL,
url_event VARCHAR(255) NOT NULL,
other VARCHAR(255) NOT NULL
);
ministral-8b-latest 0.7
Array
(
[id] => 4b898d04d004460e83bb0eff44c13914
[created] => 1758557125
[model] => ministral-8b-latest
[usage] => Array
(
[prompt_tokens] => 981
[total_tokens] => 2154
[completion_tokens] => 1173
)
[object] => chat.completion
[choices] => Array
(
[0] => Array
(
[index] => 0
[finish_reason] => stop
[message] => Array
(
[role] => assistant
[tool_calls] =>
[content] => ```json
{
"root": "Museo del 900",
"url": "https://www.museodelnovecento.org/",
"events": [
{
"name": "Presentazione “Pablo Echaurren. Art for the many”",
"startdate": "2025-10-01",
"enddate": "2025-10-01",
"price": 0,
"location": {
"name": "FORUM900",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "10:00 - 19:30",
"url": "/web/museo-del-900/-/presentazione-pablo-echaurren.-art-for-the-many-",
"other": "Evento gratuito"
},
{
"name": "Visita guidata gratuita primo giovedì del mese | ore 19.30",
"startdate": "2025-10-02",
"enddate": "2025-10-02",
"price": 0,
"location": {
"name": "EDU900 | ore 19.30",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "19:30",
"url": "/web/museo-del-900/-/visita-guidata-gratuita-primo-giovedi-del-mese-ore-19.30-2-ottobre-2025",
"other": "Evento gratuito"
},
{
"name": "Giornata del Contemporaneo 2025",
"startdate": "2025-10-04",
"enddate": "2025-10-04",
"price": 0,
"location": {
"name": "EDU900",
"city": "Milano",
"address": "Piazza Duomo, 8"
},
"transport": "Piazza Duomo, 8 Milano",
"hours": "10:00 - 19:30",
"url": "/web/museo-del-900/-/giornata-del-contemporaneo-2025",
"other": "Evento gratuito"
}
]
}
```
Ecco il codice PHP per importare i dati leggendo tanti file .json tutti nella stessa cartella:
```php
<?php
$directory = 'path/to/json/files/'; // Sostituisci con il percorso della cartella contenente i file JSON
$files = glob($directory . '*.json');
foreach ($files as $file) {
$jsonData = file_get_contents($file);
$data = json_decode($jsonData, true);
if (json_last_error() === JSON_ERROR_NONE) {
$root = $data['root'];
$url = $data['url'];
$events = $data['events'];
foreach ($events as $event) {
$name = $event['name'];
$startdate = $event['startdate'];
$enddate = $event['enddate'];
$price = $event['price'];
$location_name = $event['location']['name'];
$location_city = $event['location']['city'];
$location_address = $event['location']['address'];
$transport = $event['transport'];
$hours = $event['hours'];
$url_event = $event['url'];
$other = $event['other'];
// Inserisci i dati nel database
$conn = new mysqli('localhost', 'username', 'password', 'database_name');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO events (root, url, name, startdate, enddate, price, location_name, location_city, location_address, transport, hours, url_event, other) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssss", $root, $url, $name, $startdate, $enddate, $price, $location_name, $location_city, $location_address, $transport, $hours, $url_event, $other);
if ($stmt->execute()) {
echo "Evento inserito con successo.\n";
} else {
echo "Errore nell'inserimento dell'evento: " . $stmt->error . "\n";
}
$stmt->close();
$conn->close();
}
} else {
echo "Errore nel parsing del file JSON: " . json_last_error_msg() . "\n";
}
}
?>
```
Assicurati di sostituire `'path/to/json/files/'`, `'username'`, `'password'`, e `'database_name'` con i valori appropriati per il tuo ambiente.
)
)
)
)