title: Real time notification by using php and jquery
body: <p>I have events in which when ever new event is created I want notification to pop up for users so that they can know. I want to generate real time notification when ever any user create new event I have used php and jQuery for that but the problem I'm facing is my code is generating notification when new event arrives but for once only and for one user only. I want that every user can have notification about events but not multiple times if user had that notification then no need to show them again but if not then notification should be generated. So Here is my code:</p>
  
  <p>Notification.php:-</p>
  
  <pre><code>&lt;?php
  $user = "root";
  $pass = "";
  $host = "localhost";
  $dbname = "db_notification";
  try {
      $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e) {
      echo $e-&gt;getMessage();
  }
  $output = array(
      'found' =&gt; 0,
      'notifications' =&gt; array()
  );
  if ( ! empty($_GET['action'])) {
      switch ($_GET['action']) {
          case "show_last_five":
              $sql = "SELECT * FROM `tbl_notification` INNER JOIN    tbl_user_notification ON tbl_user_notification.notification_id =   tbl_notification.id WHERE tbl_user_notification.user_id = '1';";
              $q = $conn-&gt;query($sql);
              $q-&gt;setFetchMode(PDO::FETCH_ASSOC);
              if ($q-&gt;rowCount() &gt; 0) {
                  $output['found'] = 0;
                  $output['notifications'] = null;
              } else {
                  $sql = "SELECT * FROM `tbl_notification` LIMIT 5;";
                  $q = $conn-&gt;query($sql);
                  $q-&gt;setFetchMode(PDO::FETCH_ASSOC);
                  $output['found'] = $q-&gt;rowCount();
                  $tempArr = array();
                  $insertArray = "";
                  foreach ($q-&gt;fetchAll() as $row) {
                      array_push($tempArr, $row);
                      $sql1 = "INSERT INTO tbl_user_notification (user_id,notification_id)   VALUES('1','" . $row['id'] . "')";
                      $q1 = $conn-&gt;query($sql1);
                  }
                  $output['notifications'] = $tempArr;
              }
          break;
          default:
          break;
      }
      header('Content-Type:application/json;');
      echo json_encode($output);
  }
  ?&gt;
  </code></pre>
  
  <p>Notification.js:-</p>
  
  <pre><code>$(document).ready(function() {
      var lastDisplayed = 0;
      $('#gnrt_notfctn').on('click', function () {
          $.getJSON('http://localhost/test/test/notifications/notification.php?  action=show_last_five', function (data) {
              var output = '';
              if (data['found'] &gt; 0) {
                  $.each(data['notifications'], function (i, v) {
                      if ($('#lastNotification').text() !== v['id']) {
                          var close_button = '';
                          if (v['close_button'] == 1) {
                              close_button = "&lt;span style='position:absolute;right:25px;' data- dismiss='alert'&gt;&lt;i class='fa fa-close'&gt;&lt;/i&gt;&lt;/span&gt;";
                          } else {
                              close_button = "";
                          }
                          output = output + "&lt;div class='alert alert-" + v['type'] + "'&gt;" +
                              close_button + "&lt;h4 class='alert_heading'&gt;&lt;i class='fa fa-" + v['icon'] + "'&gt;    &lt;/i&gt;
                              &amp;nbsp;" + v['heading'] + "&lt;/h4&gt;" +
                              "&lt;p class='alert_body'&gt;" + v['notification_text'] + "&lt;/p&gt;&lt;/div&gt;";
                          $('#lastNotification').text(v['id']);
                      }
                  });
                  $('#show_notification').html(output);
              }
          });
      });
      setInterval(function () {
          $('#gnrt_notfctn').click();
      }, 3000);
  });
  </code></pre>