title: Live Notifications using JQuery and CodeIgniter
body: <p>I am using CodeIgniter, basically I have a table which has dummy data, and I am returning this data using jQuery.</p>
  
  <p>I do this by:</p>
  
  <p>Controller: </p>
  
  <pre><code>class Notifications extends CI_Controller{
          function getAlerts(){
              $query = $this-&gt;db-&gt;query("SELECT ID, subject, message, readMessage FROM data WHERE readMessage = 0");
              $result = $query-&gt;num_rows();
  
              if($result &gt; 0){
                  return $result;
                  echo $result;
              }   
          }
  </code></pre>
  
  <p>Header Script:   </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
        $(function() {
          getMessageCount1();
        });
        function getMessageCount1() {
          $('div#alertsfrommydb').load('&lt;?php echo base_url(); ?&gt;notifications/getAlerts').fadeIn();
          setTimeout("getMessageCount1()", 10000);
        }
     &lt;/script&gt;
  </code></pre>
  
  <p>and the view as such:</p>
  
  <pre><code>&lt;div id="alertsfrommydb"&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I have a method which is called showAlerts() in the same controller which prints a table and shows my messages refreshing every 10 seconds, and this works perfectly fine using above approach.</p>
  
  <p>In addition I have a method called notifications.php and I load this in my controller, which then allows me to simply in my view type:</p>
  
  <pre><code>print_r($this-&gt;notifications-&gt;getAlerts());
  </code></pre>
  
  <p>And the query shows my results fine which is '6' meaning I have 6 unread messages.</p>
  
  <p>So my argument is, the query is working, so why will it not work using the jQuery approach.</p>
  
  <p>Although my query works, I have a gut feeling there is something not right about it lol, hope someone can help. </p>
  
  <p>Thanks</p>