2017-01-11 1 views
1

저는 AJAX를 처음 접했고 배열을 얻는 방법을 모릅니다. 실시간으로 AJAX로 알림 막대를 자동으로 업데이트하려고합니다. 이것은 내가에만 카운트 AJAX 작업 얻을 경우/AJAXAJAX는 여러 배열에서 PHP 출력을 얻습니다.

$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); 
    } 
} 

을 그냥 풍선의 수를 업데이트하고있어 경우에 나는 일을했을하지만 PHP와 MySQL의에서 또한 메시지를받을 수 없습니다 하지만 AJAX를 PHP로 사용하는 방법과 루프를 얻는 방법을 알지 못합니다.

<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> 

이것은 내가하고 싶은 것입니다. 빨간색으로 작동하는 알림 수는 있지만 둘 다 작동하는 방법을 알지 못합니다. IMAGE HOW LOOK IN WEB

+6

[mysql_'] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-ex-mysql-functions-in-php) 새 코드에서 데이터베이스 확장자를 사용할 때마다 * * [새끼 고양이는 세계 어딘가에서 교살 된 상태입니다.] (http://bb.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg) ** 그것은 더 이상 사용되지 않으며 수년 동안 PHP7에서 영원히 사라졌습니다. PHP를 배우려는 분이라면'PDO' 또는'mysqli' 데이터베이스 확장과 prepared statement를 배우는 데 많은 시간을 할애하십시오. [여기에서 시작] (http://php.net/manual/en/book.pdo.php) – RiggsFolly

답변

0

보안상의 이유로 mysqli를 사용하십시오.

문제는 단지 1 개의 json 응답을 넣어야한다는 것입니다. 여기서 최소한 2를 제공해야합니다. 그런 다음 단 하나의 쿼리와 간단한 루프만으로 모든 것을 단순화 할 수 있습니다. (테스트되지 않은) 식으로 뭔가 : 그 후

$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)); 

은, 모든 값은 자바 스크립트/JQuery와의 response 변수에있을 것입니다.

+0

아약스와 관련하여 도움이 필요합니다. 도와 드릴 수 있습니까? – claww

+0

@claww 물론, 정확히 무엇이 필요합니까? – pycvalade

+0

내 첫 번째 게시물의 이미지를 확인하십시오, 내가 리프레쉬 페이지, 풍선 작업에서 번호를 실시간으로 업데이 트하려면 아약스와 알림을하고 싶어하지만 메시지를 얻을 내가 루프가 필요하고 어떻게 해야할지 모르겠다. – claww