| Server IP : 172.67.187.206 / Your IP : 172.71.28.155 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/pr/ |
Upload File : |
<?php
include 'functions.php';
check_login('admin');
$edit_mode = false;
$edit_data = null;
// 1. ลบประเภทข่าว
if(isset($_GET['del'])) {
$id = intval($_GET['del']);
mysqli_query($conn, "DELETE FROM news_categories WHERE cat_id = $id");
echo "<script>alert('ลบประเภทข่าวเรียบร้อย'); window.location='admin_categories.php';</script>";
}
// 2. ดึงข้อมูลเพื่อเตรียมแก้ไข
if(isset($_GET['edit'])) {
$id = intval($_GET['edit']);
$sql = "SELECT * FROM news_categories WHERE cat_id = $id";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
$edit_mode = true;
$edit_data = mysqli_fetch_assoc($result);
}
}
// 3. เพิ่มประเภทข่าวใหม่
if(isset($_POST['add_cat'])) {
$c = mysqli_real_escape_string($conn, $_POST['cat_name']);
mysqli_query($conn, "INSERT INTO news_categories (cat_name) VALUES ('$c')");
echo "<script>alert('เพิ่มประเภทข่าวเรียบร้อย'); window.location='admin_categories.php';</script>";
}
// 4. บันทึกการแก้ไข
if(isset($_POST['update_cat'])) {
$id = intval($_POST['cat_id']);
$c = mysqli_real_escape_string($conn, $_POST['cat_name']);
$sql = "UPDATE news_categories SET cat_name = '$c' WHERE cat_id = $id";
if(mysqli_query($conn, $sql)) {
echo "<script>alert('แก้ไขประเภทข่าวเรียบร้อย'); window.location='admin_categories.php';</script>";
} else {
echo "<script>alert('เกิดข้อผิดพลาด: " . mysqli_error($conn) . "');</script>";
}
}
echo get_header("จัดการประเภทข่าว");
?>
<div class="row">
<div class="col-md-4 mb-4">
<div class="card shadow-sm border-0 border-top border-3 <?php echo $edit_mode ? 'border-warning' : 'border-primary'; ?>">
<div class="card-body">
<h5 class="card-title <?php echo $edit_mode ? 'text-warning text-dark' : 'text-primary'; ?>">
<?php echo $edit_mode ? '<i class="bi bi-pencil-square"></i> แก้ไขประเภทข่าว' : '<i class="bi bi-tags"></i> เพิ่มประเภทข่าวใหม่'; ?>
</h5>
<form method="post">
<?php if($edit_mode) { ?>
<input type="hidden" name="cat_id" value="<?php echo $edit_data['cat_id']; ?>">
<?php } ?>
<div class="mb-3">
<label class="form-label text-muted small">ชื่อประเภทข่าว</label>
<input type="text" name="cat_name" class="form-control" placeholder="เช่น ข่าวประชาสัมพันธ์ทั่วไป"
value="<?php echo $edit_mode ? $edit_data['cat_name'] : ''; ?>" required>
</div>
<div class="d-grid gap-2">
<?php if($edit_mode) { ?>
<button type="submit" name="update_cat" class="btn btn-warning text-dark">
<i class="bi bi-save"></i> บันทึกการแก้ไข
</button>
<a href="admin_categories.php" class="btn btn-secondary">ยกเลิก</a>
<?php } else { ?>
<button type="submit" name="add_cat" class="btn btn-primary">
<i class="bi bi-plus-lg"></i> เพิ่มข้อมูล
</button>
<?php } ?>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card shadow-sm border-0">
<div class="table-responsive">
<table class="table table-hover bg-white mb-0 align-middle">
<thead class="table-light">
<tr>
<th class="ps-4">ชื่อประเภทข่าว</th>
<th class="text-center" style="width: 180px;">จัดการ</th>
</tr>
</thead>
<tbody>
<?php
$res = mysqli_query($conn, "SELECT * FROM news_categories ORDER BY cat_id ASC");
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)){
// ไฮไลท์แถวที่กำลังถูกแก้ไข
$row_class = ($edit_mode && $row['cat_id'] == $edit_data['cat_id']) ? 'table-warning' : '';
echo "<tr class='{$row_class}'>
<td class='ps-4'>{$row['cat_name']}</td>
<td class='text-center'>
<div class='btn-group btn-group-sm'>
<a href='?edit={$row['cat_id']}' class='btn btn-outline-warning text-dark' title='แก้ไข'>
<i class='bi bi-pencil'></i> แก้ไข
</a>
<a href='?del={$row['cat_id']}' onclick='return confirm(\"ยืนยันการลบประเภทข่าวนี้? (รายการข่าวที่ใช้ประเภทนี้อยู่จะไม่ถูกลบ แต่จะอ้างอิงข้อมูลไม่ได้)\")' class='btn btn-outline-danger' title='ลบ'>
<i class='bi bi-trash'></i> ลบ
</a>
</div>
</td>
</tr>";
}
} else {
echo "<tr><td colspan='2' class='text-center text-muted py-4'>ยังไม่มีข้อมูลประเภทข่าว</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php echo get_footer(); ?>