Here’s a step-by-step guide to implementing a basic notification system in CodeIgniter 3. This system will allow you to notify users (e.g., admins or reps) when specific actions occur (e.g., a school request is created, approved, or denied).
1. Database Table Structure
First, create a notifications table to store notifications. The table should have the following columns:
sql
CREATE TABLE `notifications` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL, -- ID of the user receiving the notification
`message` TEXT NOT NULL, -- Notification message
`is_read` TINYINT(1) DEFAULT 0, -- 0 = unread, 1 = read
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`related_id` INT DEFAULT NULL -- Optional: ID of the related entity (e.g., school request ID)
);
2. Create a Notification Model
Create a Notification_model to handle database interactions:
php
// application/models/Notification_model.php
class Notification_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
// Insert a new notification
public function insert_notification($user_id, $message, $related_id = null) {
$data = array(
'user_id' => $user_id,
'message' => $message,
'related_id' => $related_id
);
return $this->db->insert('notifications', $data);
}
// Get notifications for a user (optionally filtered by read status)
public function get_notifications($user_id, $read = null) {
$this->db->where('user_id', $user_id);
if ($read !== null) {
$this->db->where('is_read', $read);
}
$this->db->order_by('created_at', 'DESC');
return $this->db->get('notifications')->result_array();
}
// Mark a notification as read
public function mark_as_read($notification_id) {
$this->db->where('id', $notification_id);
$this->db->update('notifications', array('is_read' => 1));
}
// Count unread notifications for a user
public function count_unread($user_id) {
$this->db->where('user_id', $user_id);
$this->db->where('is_read', 0);
return $this->db->count_all_results('notifications');
}
}
3. Modify Existing Controllers to Send Notifications
Update your existing Schools controller to trigger notifications when actions occur.
a. When a Rep Creates a School Request
Add a notification to all admins when a school request is submitted:
php
// In your Schools controller's create_request() method
public function create_request() {
// ... existing validation and data insertion ...
if ($this->form_validation->run() == TRUE) {
// Insert school request into school_requests table (as before)
$data = array(
// ... your school request data ...
'created_by' => $this->ion_auth->user()->row()->id,
);
$this->db->insert('school_requests', $data);
// Get the created_by user (rep) ID
$rep_id = $this->ion_auth->user()->row()->id;
// Notify all admins about the new request
$this->load->model('Notification_model');
$admins = $this->get_admins(); // Implement this function to get admin user IDs
foreach ($admins as $admin_id) {
$message = "A new school request has been submitted by Rep: " . $rep_id;
$this->Notification_model->insert_notification($admin_id, $message, $this->db->insert_id());
}
// ... redirect or show success message ...
}
}
// Helper function to get admin user IDs
private function get_admins() {
$this->load->model('User_model'); // Assuming you have a User_model
return $this->User_model->get_admins(); // Implement this in your User_model
}
b. When an Admin Approves/Denies a Request
Add notifications to the rep when their request is approved/denied:
php
// In your Schools controller's approve_request() method
public function approve_request($request_id) {
// ... fetch the request and validate permissions ...
// Get the rep who created the request
$this->db->where('id', $request_id);
$request = $this->db->get('school_requests')->row_array();
$rep_id = $request['created_by'];
// Insert into schools table (your existing logic)
// ...
// Delete the request from school_requests (or archive it)
// ...
// Notify the rep
$this->load->model('Notification_model');
$message = "Your school request (ID: $request_id) has been approved!";
$this->Notification_model->insert_notification($rep_id, $message);
// ... redirect or show success message ...
}
public function deny_request($request_id) {
// Similar logic to approve_request(), but with a denial message
$message = "Your school request (ID: $request_id) has been denied. Please review the requirements.";
// ...
}
4. Display Notifications in Views
Create a view to display notifications (e.g., in a dashboard or header):
a. Display Unread Count
Add a badge in your layout to show unread notifications:
php
<!-- In your header view (e.g., application/views/includes/header.php) -->
<div class="notification-badge">
<?php
$this->load->model('Notification_model');
$unread = $this->Notification_model->count_unread($user_id); // Get current user's ID
if ($unread > 0): ?>
<span class="badge bg-danger"><?= $unread ?></span>
<?php endif; ?>
<a href="<?= base_url('notifications') ?>">Notifications</a>
</div>
b. Notifications Page
Create a Notifications controller to list all notifications for the user:
php
// application/controllers/Notifications.php
class Notifications extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Notification_model');
$this->load->helper('url');
// Ensure user is logged in
if (!$this->ion_auth->logged_in()) {
redirect('login');
}
}
public function index() {
$user_id = $this->ion_auth->user()->row()->id;
$data['notifications'] = $this->Notification_model->get_notifications($user_id);
$this->load->view('notifications/index', $data);
}
// Mark a notification as read via AJAX (optional)
public function mark_as_read() {
$id = $this->input->post('id');
$this->Notification_model->mark_as_read($id);
echo json_encode(['success' => true]);
}
}
c. Notifications View (HTML)
Create a view to display notifications (e.g., application/views/notifications/index.php):