0

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.

2 Answers2

0

All you need to do is count the number of $message_threads, right?

...
$message_threads = $this->db->get('message_thread')->result_array();
$unread_message_number = count($message_threads);

And then send $unread_message_number to your view. You put everything together in your example, I only imagine your model, view, and controller are separate in your actual app though.

Pacio
  • 523
  • 4
  • 11
  • You should probably post the relevant code then, there's no way for anyone to know how you calculate unread email – Pacio Mar 08 '17 at 14:42
0

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;?>
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25