Issue
How do I count all unread email for a specific user and put it on a badge: The files are here.
Location code/function:
- crud_model // message number apper count per user // START
- header // message number apper count per user // START
Example: The Admin have 10 message total (8 read message + 2 unread message). The badge show 2 unread message.
<?php
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('sender', $current_user);
$this->db->or_where('reciever', $current_user);
$message_threads = $this->db->get('message_thread')->result_array();
$unread_message_number = count($message_threads);
?>
<a href="<?php echo base_url();?>index.php?teacher/message">
<i class="entypo-mail"></i>
Message
<span class="badge badge-secondary"><?php echo $unread_message_number; ?></span>
</a>
</li>
<?php endif;?>
The existent controller:
function count($message_thread_code) {
$unread_message_counter = 0;
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$messages = $this->db->get_where('message', array('message_thread_code' => $message_thread_code))->result_array();
foreach ($messages as $row) {
if ($row['sender'] != $current_user && $row['read_status'] == '0')
$unread_message_counter++;
}
return $unread_message_counter;
}
I want to count the total number of unread messages from the logged user.
Solution
You should not name a method or function the same as a native PHP function like count()
. Remove the method, and instead just use Codeigniters count_all_results()
Try this:
<?php
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('message_thread.reciever', $current_user);
$this->db->where('message.read_status', 0);
$this->db->from('message_thread');
$this->db->join('message', 'message.message_thread_code = message_thread.message_thread_code');
$unread_message_number = $this->db->count_all_results();
?>
Since you do not have a reciever id in your table message, we are making a join to be able to query the current user. In my example I assume that you have the column message_thread_code
as the foreign key between the messages and the tread?
If this example works, you do not need the custom count()
method in your controller.
Alternative 2: To make the code cleaner, you could put the logic inside a method in the controller:
function count_unread(){
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('message_thread.reciever', $current_user);
$this->db->where('message.read_status', 0);
$this->db->from('message_thread');
$this->db->join('message', 'message.message_thread_code = message_thread.message_thread_code');
return $this->db->count_all_results();
}
And then remove the PHP-code and only use this in your header:
<?php if($account_type == 'teacher'):?>
<li>
<a href="<?php echo base_url();?>index.php?teacher/message">
<i class="entypo-mail"></i>
Message
<span class="badge badge-secondary"><?php echo count_unread(); ?></span>
</a>
</li>
<?php endif;?>
Answered By - Michael Krikorev Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.