1

I'm new in AJAX and I dont know how to get arrays, I'm looking to automatically update an notification bar in real time with AJAX. I have done the work if I'm just updating the number from ballon but I can't get also the messages from the MySQL with PHP/AJAX

$sql=mysql_query("select * from $tableName where is_read=0");                   
$comment_count=mysql_num_rows($sql);
$comm_array = array('count' => $comment_count);

//first output need for get number of notications
echo json_encode($comm_array);

$listsql=mysql_query("select * from notification order by execute_at desc");
while($rowsmall=mysql_fetch_array($listsql))
{ 
   $n_id=$rowsmall['notification_id'];
   $date=date("Y-m-d H:i:s", strtotime($rowsmall["execute_at"]));
   $command=$rowsmall['command'];
   $server=$rowsmall['hostname'];
   $status=$rowsmall['status'];
   $variable[] = array( 'data' => "$date", 
                    'command' => "$command",
                     'server' => "$server",
                     'status' => "$status" );
   if($rowsmall['is_read'] == 0)
   {
      //this was just a try for get with different colors the unread notification
      //second output - get notification from mysql
      echo json_encode($variable);
   }
   else 
   {
      //this was just a try for get with different colors the read notification
      //second output - get notification from mysql
      echo json_encode($variable);
   }
}

This is the AJAX and work if I get only the count but I don't know how to get with AJAX this PHP while loop and how to get both outputs ?

    <script>
$(document).ready(function() {
    setInterval("ajaxcall()",2000);
});

function ajaxcall() {
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
    $.ajax({     
    type: "GET",
    url: '/notifications/api.php',                  //the script to call to get data          
    success: function(response){          //on recieve of reply
        json_object = JSON.parse(response)
        var count = json_object.count;
        var date = json_object.notifications.data;
        var server = json_object.notifications.server;
        var command = json_object.notifications.command;
        var status = json_object.notifications.status;
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        $('#mes').html(count); //Set output element html
        $('#not_read').html("Date: "+date+" Host: "+server+" Command: "+command+" Status: "+status);
        //recommend reading up on jquery selectors they are awesome 
        // http://api.jquery.com/category/selectors/
    } 
});
}
</script>

This is what i want to do. with red is the number of alerts which works but i dont know how to make both work IMAGE HOW LOOK IN WEB

  • 6
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 11 '17 at 18:04

1 Answers1

0

Please use mysqli for security reasons.

Your problem lies in the fact that you need to put only 1 json response, here you supply at least 2. Then, you could simplify all that with just 1 query and a simple loop. Something like (untested):

    $objMysql = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

    $r = array();
    $comment_count = 0;
    if(!$objMysql->connect_error){
      $strQuery = "select a.num as comment_count, b.* from
      (select count(*) as num from {$tableName} where is_read=0) as a,
      (select * from notification order by execute_at desc) as b";

      $res = $objMysql->query($strQuery);

      while($row = mysql_fetch_assoc($res)){
        $comment_count = $row['comment_count'];

        $r[] = array(
          'data' => date("Y-m-d H:i:s", strtotime($rowsmall["execute_at"])),
          'command' => $row['command'],
          'server' => $row['hostname'],
          'status' => $row['status'],
          'color' => ($row['is_read'] == 0)? 'red' : 'blue'
        );
      }

    }else{
      $r[] = 'mysql connect error: '.$objMysql->connect_error;
    }
    echo json_encode(array('comment_count' => $comment_count, 'results' => $r));

After that, all your values will be in the response variable of your javascript/jquery.

pycvalade
  • 181
  • 9