
PK 
<?php
ob_start(); // IMPORTANT: Prevents header issues
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('../includes/auth.php');
include('../../includes/db.php');
// =========================
// HANDLE ADD CATEGORY
// =========================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_category'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$slug = mysqli_real_escape_string($conn, strtolower(str_replace(' ', '-', $_POST['slug'] ?: $_POST['name'])));
// Check duplicate slug
$check_slug = $conn->query("SELECT id FROM categories WHERE slug = '$slug'");
if ($check_slug->num_rows > 0) {
$slug = $slug . '-' . time();
}
$stmt = $conn->prepare("INSERT INTO categories (name, slug) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $slug);
if ($stmt->execute()) {
$_SESSION['success'] = "Category added successfully!";
} else {
$_SESSION['error'] = "Error adding category: " . $conn->error;
}
header('Location: categories.php');
exit();
}
// =========================
// HANDLE EDIT CATEGORY
// =========================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit_category'])) {
$id = intval($_POST['category_id']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$slug = mysqli_real_escape_string($conn, strtolower(str_replace(' ', '-', $_POST['slug'] ?: $_POST['name'])));
// Check duplicate slug except current
$check_slug = $conn->query("SELECT id FROM categories WHERE slug = '$slug' AND id != $id");
if ($check_slug->num_rows > 0) {
$slug = $slug . '-' . time();
}
$stmt = $conn->prepare("UPDATE categories SET name=?, slug=? WHERE id=?");
$stmt->bind_param("ssi", $name, $slug, $id);
if ($stmt->execute()) {
$_SESSION['success'] = "Category updated successfully!";
} else {
$_SESSION['error'] = "Error updating category: " . $conn->error;
}
header('Location: categories.php');
exit();
}
// =========================
// HANDLE DELETE CATEGORY
// =========================
if (isset($_GET['delete'])) {
$id = intval($_GET['delete']);
// Check if category has packages
$check = $conn->query("SELECT COUNT(*) as count FROM packages WHERE category_id = $id");
$count = $check->fetch_assoc()['count'];
if ($count > 0) {
$_SESSION['error'] = "Cannot delete category! It has $count package(s) associated with it.";
} else {
$conn->query("DELETE FROM categories WHERE id = $id");
$_SESSION['success'] = "Category deleted successfully!";
}
header('Location: categories.php');
exit();
}
// =========================
// FETCH DATA
// =========================
// Get all categories
$categories = $conn->query("SELECT * FROM categories ORDER BY id DESC");
// Get category for editing
$edit_category = null;
if (isset($_GET['edit'])) {
$id = intval($_GET['edit']);
$edit_category = $conn->query("SELECT * FROM categories WHERE id = $id")->fetch_assoc();
}
// Function to count packages
function getPackageCount($conn, $category_id) {
$result = $conn->query("SELECT COUNT(*) as count FROM packages WHERE category_id = $category_id");
return $result->fetch_assoc()['count'];
}
// =========================
// LOAD UI AFTER LOGIC
// =========================
include('../includes/header.php');
include('../includes/sidebar.php');
?>
<style>
.sidebar {
background: #2c3e50;
min-height: 100vh;
padding: 20px;
}
.sidebar a {
color: white;
text-decoration: none;
display: block;
padding: 10px;
margin: 5px 0;
border-radius: 5px;
transition: all 0.3s;
}
.sidebar a:hover, .sidebar a.active {
background: #34495e;
}
.sidebar a i {
margin-right: 10px;
}
.content-area {
padding: 20px;
background: #f5f7fb;
min-height: 100vh;
}
.card {
border: none;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0,0,0,0.05);
margin-bottom: 30px;
}
.card-header {
background: white;
border-bottom: 1px solid #e0e0e0;
padding: 20px;
font-weight: bold;
font-size: 18px;
}
.table-categories {
background: white;
border-radius: 10px;
overflow: hidden;
}
.table-categories th {
background: #f8f9fa;
border-bottom: 2px solid #e0e0e0;
}
.btn-icon {
padding: 5px 10px;
margin: 0 2px;
}
.form-label {
font-weight: 500;
margin-bottom: 8px;
}
.required:after {
content: " *";
color: red;
}
.slug-code {
background: #f8f9fa;
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
font-size: 12px;
}
</style>
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<!-- <div class="col-md-2 sidebar">
<h4 class="text-white mb-4">
<i class="fas fa-travel"></i> YatraDham
</h4>
<a href="dashboard.php">
<i class="fas fa-tachometer-alt"></i> Dashboard
</a>
<a href="categories.php" class="active">
<i class="fas fa-tags"></i> Categories
</a>
<a href="add_package.php">
<i class="fas fa-plus-circle"></i> Add Package
</a>
<a href="manage_packages.php">
<i class="fas fa-boxes"></i> Manage Packages
</a>
<a href="bookings.php">
<i class="fas fa-calendar-check"></i> Bookings
</a>
<hr style="border-color: #34495e;">
<a href="logout.php">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
</div> -->
<!-- Main Content -->
<div class="col-12 content-area">
<!-- Alert Messages -->
<?php if(isset($_SESSION['success'])): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="fas fa-check-circle"></i> <?php echo $_SESSION['success']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if(isset($_SESSION['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="fas fa-exclamation-circle"></i> <?php echo $_SESSION['error']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<!-- Add/Edit Category Form -->
<div class="card">
<div class="card-header">
<i class="fas fa-<?php echo $edit_category ? 'edit' : 'plus'; ?>"></i>
<?php echo $edit_category ? 'Edit Category' : 'Add New Category'; ?>
</div>
<div class="card-body">
<form method="POST" action="">
<?php if($edit_category): ?>
<input type="hidden" name="category_id" value="<?php echo $edit_category['id']; ?>">
<input type="hidden" name="edit_category" value="1">
<?php else: ?>
<input type="hidden" name="add_category" value="1">
<?php endif; ?>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label required">Category Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $edit_category ? htmlspecialchars($edit_category['name']) : ''; ?>" required>
<small class="text-muted">This will be displayed on the website</small>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Slug (URL)</label>
<input type="text" name="slug" class="form-control" value="<?php echo $edit_category ? htmlspecialchars($edit_category['slug']) : ''; ?>" placeholder="auto-generated from name">
<small class="text-muted">Leave empty to auto-generate from name</small>
</div>
</div>
<button type="submit" class="btn btn-primary">
<i class="fas fa-save"></i> <?php echo $edit_category ? 'Update Category' : 'Add Category'; ?>
</button>
<?php if($edit_category): ?>
<a href="categories.php" class="btn btn-secondary">
<i class="fas fa-times"></i> Cancel
</a>
<?php endif; ?>
</form>
</div>
</div>
<!-- Categories List -->
<div class="card">
<div class="card-header">
<i class="fas fa-list"></i> All Categories
<span class="badge bg-primary float-end">Total: <?php echo $categories->num_rows; ?></span>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-categories mb-0">
<thead>
<tr>
<th width="50">ID</th>
<th>Category Name</th>
<th>Slug</th>
<th>Packages</th>
<th>Created Date</th>
<th width="150">Actions</th>
</tr>
</thead>
<tbody>
<?php if($categories->num_rows > 0): ?>
<?php while($cat = $categories->fetch_assoc()):
$package_count = getPackageCount($conn, $cat['id']);
?>
<tr>
<td><?php echo $cat['id']; ?></td>
<td>
<strong><?php echo htmlspecialchars($cat['name']); ?></strong>
</td>
<td><span class="slug-code"><?php echo $cat['slug']; ?></span></td>
<td>
<span class="badge bg-info"><?php echo $package_count; ?> packages</span>
</td>
<td><?php echo date('d-m-Y', strtotime($cat['created_at'])); ?></td>
<td>
<a href="?edit=<?php echo $cat['id']; ?>" class="btn btn-sm btn-warning btn-icon" title="Edit">
<i class="fas fa-edit"></i>
</a>
<?php if($package_count == 0): ?>
<a href="?delete=<?php echo $cat['id']; ?>" class="btn btn-sm btn-danger btn-icon" title="Delete" onclick="return confirm('Are you sure you want to delete this category?')">
<i class="fas fa-trash"></i>
</a>
<?php else: ?>
<button class="btn btn-sm btn-secondary btn-icon" disabled title="Cannot delete - has packages">
<i class="fas fa-lock"></i>
</button>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="6" class="text-center py-5">
<i class="fas fa-folder-open fa-3x text-muted mb-3 d-block"></i>
<p>No categories found. Create your first category above!</p>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Statistics -->
<div class="row mt-4">
<div class="col-md-4">
<div class="card bg-primary text-white">
<div class="card-body">
<h5 class="card-title">Total Categories</h5>
<h2><?php echo $conn->query("SELECT COUNT(*) as count FROM categories")->fetch_assoc()['count']; ?></h2>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card bg-success text-white">
<div class="card-body">
<h5 class="card-title">Total Packages</h5>
<h2><?php echo $conn->query("SELECT COUNT(*) as count FROM packages")->fetch_assoc()['count']; ?></h2>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card bg-info text-white">
<div class="card-body">
<h5 class="card-title">Categories with Packages</h5>
<h2><?php echo $conn->query("SELECT COUNT(DISTINCT category_id) as count FROM packages WHERE category_id IS NOT NULL")->fetch_assoc()['count']; ?></h2>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Auto-generate slug from name
$('input[name="name"]').on('keyup', function() {
var slugInput = $('input[name="slug"]');
if(slugInput.val() === '') {
var slug = $(this).val()
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
slugInput.val(slug);
}
});
</script>


PK 99