mirror of
https://github.com/Alvin-Zilverstand/pokedex.git
synced 2026-03-06 11:07:31 +01:00
Refactor Pokémon detail display and improve error handling in data fetching
This commit is contained in:
@@ -17,7 +17,10 @@ $conn = new mysqli($servername, $username, $password, $dbname);
|
||||
|
||||
if ($conn->connect_error) {
|
||||
logMessage("Connection failed: " . $conn->connect_error);
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
header("Content-Type: application/json");
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Database connection failed"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
header("Cache-Control: max-age=86400"); // Cache for 24 hours
|
||||
@@ -25,7 +28,17 @@ header("Content-Type: application/json"); // Ensure JSON response
|
||||
|
||||
if (isset($_GET['id'])) {
|
||||
$id = intval($_GET['id']);
|
||||
$sql = "SELECT p.*, s.flavor_text, GROUP_CONCAT(t.name) AS types, GROUP_CONCAT(a.name) AS abilities, st.hp, st.attack, st.defense, st.sp_attack, st.sp_defense, st.speed
|
||||
if ($id <= 0) {
|
||||
logMessage("Invalid Pokémon ID: $id");
|
||||
echo json_encode(["error" => "Invalid Pokémon ID"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "SELECT p.*,
|
||||
COALESCE(s.flavor_text, '') AS flavor_text,
|
||||
GROUP_CONCAT(DISTINCT t.name) AS types,
|
||||
GROUP_CONCAT(DISTINCT a.name) AS abilities,
|
||||
st.hp, st.attack, st.defense, st.sp_attack, st.sp_defense, st.speed
|
||||
FROM pokemon p
|
||||
LEFT JOIN species s ON p.id = s.pokemon_id
|
||||
LEFT JOIN pokemon_types pt ON p.id = pt.pokemon_id
|
||||
@@ -38,37 +51,41 @@ if (isset($_GET['id'])) {
|
||||
logMessage("Executing query: $sql");
|
||||
$result = $conn->query($sql);
|
||||
|
||||
if ($result) {
|
||||
if ($result->num_rows > 0) {
|
||||
$pokemon = $result->fetch_assoc();
|
||||
$pokemon['types'] = explode(',', $pokemon['types']);
|
||||
$pokemon['abilities'] = explode(',', $pokemon['abilities']);
|
||||
logMessage("Query result: " . json_encode($pokemon));
|
||||
echo json_encode($pokemon);
|
||||
} else {
|
||||
logMessage("No Pokémon found for ID: $id");
|
||||
echo json_encode(["error" => "No Pokémon found"]);
|
||||
}
|
||||
} else {
|
||||
if ($result === false) {
|
||||
logMessage("Error executing query: " . $conn->error);
|
||||
echo json_encode(["error" => "Error fetching Pokémon data"]);
|
||||
echo json_encode(["error" => "Error executing query"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($result->num_rows > 0) {
|
||||
$pokemon = $result->fetch_assoc();
|
||||
$pokemon['types'] = $pokemon['types'] ? explode(',', $pokemon['types']) : [];
|
||||
$pokemon['abilities'] = $pokemon['abilities'] ? explode(',', $pokemon['abilities']) : [];
|
||||
logMessage("Query result: " . json_encode($pokemon));
|
||||
echo json_encode($pokemon);
|
||||
} else {
|
||||
logMessage("No Pokémon found for ID: $id");
|
||||
echo json_encode(["error" => "No Pokémon found"]);
|
||||
}
|
||||
} else {
|
||||
$sql = "SELECT * FROM pokemon";
|
||||
// Handle requests without an 'id' parameter
|
||||
$sql = "SELECT id, name, height, weight, base_experience, image_url FROM pokemon";
|
||||
logMessage("Executing query: $sql");
|
||||
$result = $conn->query($sql);
|
||||
|
||||
if ($result) {
|
||||
$pokemons = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$pokemons[] = $row;
|
||||
}
|
||||
logMessage("Query result: " . json_encode($pokemons));
|
||||
echo json_encode($pokemons);
|
||||
} else {
|
||||
if ($result === false) {
|
||||
logMessage("Error executing query: " . $conn->error);
|
||||
echo json_encode(["error" => "Error fetching Pokémon data"]);
|
||||
echo json_encode(["error" => "Error executing query"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$pokemons = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$pokemons[] = $row;
|
||||
}
|
||||
|
||||
logMessage("Query result: " . json_encode($pokemons));
|
||||
echo json_encode($pokemons);
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
|
||||
Reference in New Issue
Block a user