prepare(" INSERT IGNORE INTO consultant_clients (consultant_id, client_id, assigned_by) VALUES (?, ?, ?) ")->execute([$consultantId, $clientId, getCurrentUserId()]); $flash = 'Client assigned successfully.'; } catch (PDOException $e) { $flash = 'Assignment failed: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); $flashType = 'danger'; } } else { $flash = 'Please select both a consultant and a client.'; $flashType = 'warning'; } } elseif ($action === 'remove') { $assignmentId = (int) ($_POST['assignment_id'] ?? 0); if ($assignmentId) { $pdo->prepare("DELETE FROM consultant_clients WHERE id = ?")->execute([$assignmentId]); $flash = 'Assignment removed.'; } } elseif ($action === 'set_type') { $targetUserId = (int) ($_POST['user_id'] ?? 0); $userType = $_POST['user_type'] ?? ''; if ($targetUserId && in_array($userType, ['client', 'consultant', 'admin'], true)) { $pdo->prepare("UPDATE users SET user_type = ? WHERE id = ?") ->execute([$userType, $targetUserId]); $flash = 'User type updated.'; } else { $flash = 'Invalid user type selection.'; $flashType = 'warning'; } } } } // ── Load data ───────────────────────────────────────────────────────────────── // All users with their type $allUsers = $pdo->query(" SELECT id, fullname, email, user_type FROM users WHERE active = 1 ORDER BY user_type, fullname ")->fetchAll(); // Consultants with their assigned clients $consultants = $pdo->query(" SELECT u.id, u.fullname, u.email FROM users u WHERE u.user_type IN ('consultant','admin') AND u.active = 1 ORDER BY u.fullname ")->fetchAll(); // All client records (for the assign dropdown) $allClients = $pdo->query(" SELECT id, client, company FROM client_records ORDER BY client ASC ")->fetchAll(); // Assignments: consultant_id → [{assignment_id, client_id, client_name, company, assigned_at}] $assignmentRows = $pdo->query(" SELECT cc.id AS assignment_id, cc.consultant_id, cc.assigned_at, cr.id AS client_id, cr.client, cr.company FROM consultant_clients cc JOIN client_records cr ON cr.id = cc.client_id ORDER BY cc.consultant_id, cr.client ")->fetchAll(); $assignmentsByConsultant = []; foreach ($assignmentRows as $row) { $assignmentsByConsultant[$row['consultant_id']][] = $row; } // ── Page render ─────────────────────────────────────────────────────────────── $pageTitle = 'Manage Consultants'; $siteName = 'Crop Monitor'; include __DIR__ . '/../../layouts/header.php'; include __DIR__ . '/../../layouts/navbar.php'; ?>

User Roles
Name Email Role
'danger', 'consultant' => 'info', default => 'secondary', }; ?>
Assign Client to Consultant
No consultant accounts yet. Set a user's role to "Consultant" using the panel on the left.
client
No clients assigned yet.
  • Assigned