File "fetch_users.php"
Full path: /home/mysamm/public_html/mysamm_login/fetch_users.php
File
size: 3.38 B
MIME-type: text/x-php; charset=us-ascii
Charset: utf-8
Download Open Edit Advanced Editor &nnbsp; Back
<?php
require_once "/home/mysamm/price_checker/lib/connections.php";
session_start();
// Ensure only admins can access this
if (isset($_SESSION['user_type']) && $_SESSION['user_type'] !== 'superadmin') {
header("Location: login.php");
exit();
}
$db = new Connection();
// Get DataTables request parameters
$start = isset($_GET['start']) ? intval($_GET['start']) : 0;
$length = isset($_GET['length']) ? intval($_GET['length']) : 10;
$searchValue = isset($_GET['search']['value']) ? trim($_GET['search']['value']) : '';
$orderColumnIndex = isset($_GET['order'][0]['column']) ? intval($_GET['order'][0]['column']) : 0;
$orderDir = isset($_GET['order'][0]['dir']) && in_array(strtolower($_GET['order'][0]['dir']), ['asc', 'desc']) ? $_GET['order'][0]['dir'] : 'desc';
// Define column mappings
$columns = ['id', 'name', 'email', 'user_type', 'ip_address', 'last_login', 'created_at', 'status'];
$orderColumn = isset($columns[$orderColumnIndex]) ? $columns[$orderColumnIndex] : 'id';
// Base query
$query = "SELECT * FROM admin_users WHERE 1";
// Apply search filter
if (!empty($searchValue)) {
$query .= " AND (name LIKE '%$searchValue%'
OR email LIKE '%$searchValue%'
OR user_type LIKE '%$searchValue%'
OR ip_address LIKE '%$searchValue%')";
}
// Get total records
$totalRecordsQuery = $db->Query("SELECT COUNT(*) as total FROM admin_users");
$totalRecords = $totalRecordsQuery[0]['total'];
// Get filtered records count
$filteredQuery = $db->Query("SELECT COUNT(*) as total FROM ($query) as filtered");
$filteredRecords = $filteredQuery[0]['total'];
// Apply ordering and pagination
$query .= " ORDER BY $orderColumn $orderDir LIMIT $start, $length";
$users = $db->Query($query);
// Prepare response
$data = [];
foreach ($users as $user) {
$data[] = [
"id" => $user['id'],
"name" => htmlspecialchars(ucfirst($user['name'])),
"email" => htmlspecialchars($user['email']),
"user_type" => ($user['user_type'] == 'admin')
? '<span class="badge bg-dark text-white">' . ucfirst($user['user_type']) . '</span>'
: '<span class="badge bg-info text-white">' . ucfirst($user['user_type']) . '</span>',
"ip_address" => htmlspecialchars($user['ip_address']),
"last_login" => date("F j, Y, g:i a", strtotime($user['last_login'])),
"created_updated" => "<strong>C:</strong> " . date("F j, Y, g:i a", strtotime($user['created_at'])) .
"<br><strong>U:</strong> " . date("F j, Y, g:i a", strtotime($user['updated_at'])),
"status" => ($user['status'] == 0)
? '<span class="badge bg-success text-white">Active</span>'
: '<span class="badge bg-danger text-white">Deactivated</span>',
"actions" => ($user['id'] != 1)
? '<a href="edit_user.php?id=' . $user['id'] . '" class="btn btn-warning btn-sm">Edit</a>
<a href="delete_user.php?id=' . $user['id'] . '" class="btn btn-danger btn-sm" onclick="return confirm(\'Are you sure you want to delete this user?\');">Delete</a>'
: '<span class="text-muted">Super Admin</span>'
];
}
// Send JSON response
echo json_encode([
"draw" => isset($_GET['draw']) ? intval($_GET['draw']) : 1,
"recordsTotal" => $totalRecords,
"recordsFiltered" => $filteredRecords,
"data" => $data
]);
?>