Editing: /home/goacom/public_html/import_games.php
<?php session_start(); // --- DATABASE CONFIGURATION --- $db_host = 'localhost'; $db_name = 'tishanw1_melbets'; $db_user = 'tishanw1_melbets'; $db_pass = 'tishanw1_melbets'; $conn = new mysqli($db_host, $db_user, $db_pass, $db_name); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Helper: Fetch all categories for dropdowns function getCategories($conn) { $res = $conn->query("SELECT * FROM categories ORDER BY id ASC"); return $res->fetch_all(MYSQLI_ASSOC); } $categories = getCategories($conn); $message = ""; $msg_type = ""; // --- HANDLE ACTIONS --- // 1. DELETE GAME if (isset($_POST['delete_game'])) { $id = intval($_POST['game_id']); $stmt = $conn->prepare("DELETE FROM games WHERE id = ?"); $stmt->bind_param("i", $id); if($stmt->execute()){ $message = "Game deleted successfully."; $msg_type = "success"; } else { $message = "Error deleting game."; $msg_type = "error"; } } // 2. SAVE GAME (ADD or UPDATE) if (isset($_POST['save_game'])) { $id = isset($_POST['game_id']) ? intval($_POST['game_id']) : 0; $g_uid = $_POST['game_uid']; $name = $_POST['name']; $image = $_POST['image']; $category_id = intval($_POST['category_id']); $game_type = $_POST['game_type']; $status = isset($_POST['status']) ? 1 : 0; // Auto-fill provider_logo based on selected category $cat_query = "SELECT icon FROM categories WHERE id = ?"; $stmt_cat = $conn->prepare($cat_query); $stmt_cat->bind_param("i", $category_id); $stmt_cat->execute(); $cat_res = $stmt_cat->get_result(); $cat_row = $cat_res->fetch_assoc(); $provider_logo = ($cat_row && isset($cat_row['icon'])) ? $cat_row['icon'] : NULL; $stmt_cat->close(); if ($id > 0) { // Update $sql = "UPDATE games SET game_uid=?, name=?, image=?, category_id=?, game_type=?, status=?, provider_logo=? WHERE id=?"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssisii", $g_uid, $name, $image, $category_id, $game_type, $status, $provider_logo, $id); } else { // Insert $sql = "INSERT INTO games (game_uid, name, image, category_id, game_type, status, provider_logo, url, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, '#', NOW())"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssis i", $g_uid, $name, $image, $category_id, $game_type, $status, $provider_logo); } if ($stmt->execute()) { $message = "Game saved successfully."; $msg_type = "success"; } else { $message = "Error saving game: " . $conn->error; $msg_type = "error"; } $stmt->close(); } // 3. DELETE CATEGORY if (isset($_POST['delete_category'])) { $id = intval($_POST['cat_id']); // Check if category has games $check = $conn->query("SELECT COUNT(*) as c FROM games WHERE category_id = $id"); $row = $check->fetch_assoc(); if($row['c'] > 0){ $message = "Cannot delete category. It contains games."; $msg_type = "error"; } else { $stmt = $conn->prepare("DELETE FROM categories WHERE id = ?"); $stmt->bind_param("i", $id); if($stmt->execute()){ $message = "Category deleted."; $msg_type = "success"; // Refresh categories list $categories = getCategories($conn); } } } // 4. ADD CATEGORY if (isset($_POST['add_category'])) { $name = $_POST['cat_name']; $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name))); $icon = $_POST['cat_icon']; $sort_order = intval($_POST['cat_sort']); $sql = "INSERT INTO categories (name, slug, icon, sort_order) VALUES (?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssi", $name, $slug, $icon, $sort_order); if ($stmt->execute()) { $message = "Category added."; $msg_type = "success"; $categories = getCategories($conn); } else { $message = "Error adding category."; $msg_type = "error"; } } // 5. IMPORT JSON if (isset($_FILES['json_file']) && $_FILES['json_file']['error'] == 0) { $category_id = intval($_POST['provider_id']); if ($category_id > 0) { // Get Logo $cat_query = "SELECT icon FROM categories WHERE id = ?"; $stmt_cat = $conn->prepare($cat_query); $stmt_cat->bind_param("i", $category_id); $stmt_cat->execute(); $cat_row = $stmt_cat->get_result()->fetch_assoc(); $provider_logo = ($cat_row && isset($cat_row['icon'])) ? $cat_row['icon'] : NULL; $stmt_cat->close(); $json_content = file_get_contents($_FILES['json_file']['tmp_name']); $data = json_decode($json_content, true); if (json_last_error() === JSON_ERROR_NONE) { $games_list = (isset($data['game_uid'])) ? [$data] : $data; $sql = "INSERT INTO games (game_uid, category_id, game_type, name, image, status, url, provider_logo, created_at) VALUES (?, ?, ?, ?, ?, ?, '#', ?, NOW()) ON DUPLICATE KEY UPDATE name=VALUES(name), image=VALUES(image), status=VALUES(status), game_type=VALUES(game_type), provider_logo=VALUES(provider_logo)"; $stmt = $conn->prepare($sql); $count = 0; foreach ($games_list as $game) { $g_uid = $game['game_uid'] ?? ''; $g_type = $game['game_type'] ?? 'Slot'; $g_name = $game['game_name'] ?? 'Unknown'; $g_image = $game['game_icon'] ?? ''; $g_status = $game['status'] ?? 1; $stmt->bind_param("sisssis", $g_uid, $category_id, $g_type, $g_name, $g_image, $g_status, $provider_logo); if($stmt->execute()) $count++; } $stmt->close(); $message = "Imported $count games."; $msg_type = "success"; } else { $message = "Invalid JSON."; $msg_type = "error"; } } } // Determine Current Page $page = isset($_GET['page']) ? $_GET['page'] : 'list_games'; // Fetch Data for Edit $edit_game = null; if ($page == 'edit_game' && isset($_GET['id'])) { $stmt = $conn->prepare("SELECT * FROM games WHERE id = ?"); $stmt->bind_param("i", $_GET['id']); $stmt->execute(); $edit_game = $stmt->get_result()->fetch_assoc(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Game Admin Panel</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f6f9; margin: 0; padding: 0; } .navbar { background-color: #343a40; padding: 15px; color: white; display: flex; justify-content: space-between; align-items: center; } .navbar h1 { margin: 0; font-size: 1.2rem; } .nav-links a { color: #ddd; text-decoration: none; margin-left: 20px; font-weight: 500; transition: 0.3s; } .nav-links a:hover, .nav-links a.active { color: #fff; border-bottom: 2px solid #28a745; } .container { max-width: 1200px; margin: 30px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); } h2 { border-bottom: 2px solid #eee; padding-bottom: 10px; margin-top: 0; } .btn { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; font-size: 14px; } .btn-primary { background-color: #007bff; color: white; } .btn-success { background-color: #28a745; color: white; } .btn-danger { background-color: #dc3545; color: white; } .btn-warning { background-color: #ffc107; color: black; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f8f9fa; font-weight: 600; } tr:hover { background-color: #f1f1f1; } .thumb-img { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; background: #eee; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; font-weight: 600; } input, select { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } .message { padding: 15px; border-radius: 5px; margin-bottom: 20px; } .success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; } .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; } .card { border: 1px solid #ddd; border-radius: 8px; padding: 15px; background: #fff; } .card h3 { margin-top: 0; font-size: 1rem; } </style> </head> <body> <nav class="navbar"> <h1>Admin Panel</h1> <div class="nav-links"> <a href="?page=list_games" class="<?php echo $page=='list_games'?'active':''; ?>">All Games</a> <a href="?page=add_game" class="<?php echo $page=='add_game'?'active':''; ?>">Add Game</a> <a href="?page=import" class="<?php echo $page=='import'?'active':''; ?>">Import JSON</a> <a href="?page=list_categories" class="<?php echo $page=='list_categories'?'active':''; ?>">Categories</a> </div> </nav> <div class="container"> <?php if ($message): ?> <div class="message <?php echo $msg_type; ?>"><?php echo $message; ?></div> <?php endif; ?> <?php if ($page == 'list_games'): ?> <h2>Manage Games</h2> <p><a href="?page=add_game" class="btn btn-primary">+ Add New Game</a></p> <table> <thead> <tr> <th>Img</th> <th>Name</th> <th>Provider (Cat)</th> <th>Type</th> <th>Status</th> <th>Actions</th> </tr> </thead> <tbody> <?php $result = $conn->query("SELECT g.*, c.name as cat_name FROM games g LEFT JOIN categories c ON g.category_id = c.id ORDER BY g.id DESC"); while($row = $result->fetch_assoc()): ?> <tr> <td><img src="<?php echo htmlspecialchars($row['image']); ?>" class="thumb-img" onerror="this.src='https://via.placeholder.com/40'"></td> <td><?php echo htmlspecialchars($row['name']); ?></td> <td><?php echo htmlspecialchars($row['cat_name'] ?? 'Unknown'); ?></td> <td><?php echo htmlspecialchars($row['game_type']); ?></td> <td><?php echo $row['status'] ? '<span style="color:green">Active</span>' : '<span style="color:red">Inactive</span>'; ?></td> <td> <a href="?page=edit_game&id=<?php echo $row['id']; ?>" class="btn btn-warning" style="font-size:12px;">Edit</a> <form method="POST" style="display:inline;" onsubmit="return confirm('Delete this game?');"> <input type="hidden" name="game_id" value="<?php echo $row['id']; ?>"> <button type="submit" name="delete_game" class="btn btn-danger" style="font-size:12px;">Delete</button> </form> </td> </tr> <?php endwhile; ?> </tbody> </table> <?php elseif ($page == 'add_game' || $page == 'edit_game'): ?> <h2><?php echo $page == 'edit_game' ? 'Edit Game' : 'Add New Game'; ?></h2> <form method="POST"> <?php if($edit_game): ?><input type="hidden" name="game_id" value="<?php echo $edit_game['id']; ?>"><?php endif; ?> <div class="form-group"> <label>Game UID (Unique ID)</label> <input type="text" name="game_uid" value="<?php echo $edit_game['game_uid'] ?? ''; ?>" required> </div> <div class="form-group"> <label>Game Name</label> <input type="text" name="name" value="<?php echo $edit_game['name'] ?? ''; ?>" required> </div> <div class="form-group"> <label>Image URL</label> <input type="text" name="image" value="<?php echo $edit_game['image'] ?? ''; ?>" placeholder="https://..." required> </div> <div class="form-group"> <label>Provider (Category)</label> <!-- This dropdown is now Dynamic --> <select name="category_id" required> <?php foreach($categories as $cat): ?> <option value="<?php echo $cat['id']; ?>" <?php echo ($edit_game && $edit_game['category_id'] == $cat['id']) ? 'selected' : ''; ?>> <?php echo htmlspecialchars($cat['name']); ?> </option> <?php endforeach; ?> </select> <small>Provider Logo will be auto-filled based on this selection.</small> </div> <div class="form-group"> <label>Game Type</label> <input type="text" name="game_type" value="<?php echo $edit_game['game_type'] ?? 'Slot'; ?>"> </div> <div class="form-group"> <label>Status</label> <input type="checkbox" name="status" value="1" <?php echo ($edit_game && $edit_game['status'] == 1) || !$edit_game ? 'checked' : ''; ?>> Active </div> <button type="submit" name="save_game" class="btn btn-success">Save Game</button> <a href="?page=list_games" class="btn btn-danger">Cancel</a> </form> <?php elseif ($page == 'import'): ?> <h2>Import Games from JSON</h2> <form method="POST" enctype="multipart/form-data"> <div class="form-group"> <label>Select Provider (Category)</label> <select name="provider_id" required> <?php foreach($categories as $cat): ?> <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label>JSON File</label> <input type="file" name="json_file" accept=".json,.txt" required> </div> <button type="submit" class="btn btn-primary">Import Now</button> </form> <?php elseif ($page == 'list_categories'): ?> <h2>Manage Categories</h2> <div style="margin-bottom: 20px; padding: 15px; background: #f9f9f9; border: 1px solid #eee;"> <h3>Add New Category</h3> <form method="POST" style="display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end;"> <div style="flex: 1;"> <label>Name</label> <input type="text" name="cat_name" required> </div> <div style="flex: 2;"> <label>Icon URL</label> <input type="text" name="cat_icon" placeholder="/assets/logo.png"> </div> <div style="width: 80px;"> <label>Sort</label> <input type="number" name="cat_sort" value="0"> </div> <button type="submit" name="add_category" class="btn btn-success" style="height: 42px;">Add</button> </form> </div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Slug</th> <th>Icon</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($categories as $cat): ?> <tr> <td><?php echo $cat['id']; ?></td> <td><?php echo htmlspecialchars($cat['name']); ?></td> <td><?php echo htmlspecialchars($cat['slug']); ?></td> <td><?php echo htmlspecialchars($cat['icon']); ?></td> <td> <form method="POST" style="display:inline;" onsubmit="return confirm('Delete this category?');"> <input type="hidden" name="cat_id" value="<?php echo $cat['id']; ?>"> <button type="submit" name="delete_category" class="btn btn-danger" style="font-size:12px;">Delete</button> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </body> </html>
💾 Save