2

未読メッセージのカウンタ統計を取得しようとしています: PHP コード (BubbleStat.php) は次のようになります。

$totalMsg = $mysql->totalRows("SELECT msg_id from messages WHERE msg_opened = 0 AND msg_receiver = '".$_SESSION["ActiveUserSessionId"]."'");
    echo $totalMsgs;

私がこれを持っているjQueryコード:

$.ajax({
type: "POST",
url: '/BubbleStat.php',
cache: false,
success: function(html)
    {
        $("#Bubble_Msg").show(); 
    } 
});

#Bubble_Msg で未読メッセージのカウンターを取得するにはどうすればよいですか? div #Bubble_Msg を非表示にするために、カウンタが未読メッセージを持っていないといいでしょう。

何か案が?

4

4 に答える 4

1

PHP スクリプトが JSON 応答を返すようにすることができます。

大量のコードのように見えるかもしれませんが、スクリプトに複雑さを追加する必要がある場合は、間違いなく価値があります。

1-何が起こっても応答がキャッシュされないようにします。

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

2-json タイプのヘッダー:

header('Content-type: application/json');

3- 知る必要があるすべての値を計算します。

$totalMsg = $mysql->totalRows("SELECT msg_id from messages WHERE msg_opened = 0 AND msg_receiver = '".$_SESSION["ActiveUserSessionId"]."'");

4- それらを使用してアレイを構築します。

$response = array(
    'total' => $totalMsg,
    'extra' => 'extra value (if needed)'
);

5-jsonエンコーディングでエコー:

echo json_encode($response);

その後、次のように jQuery を使用して値にアクセスできます。

$.ajax({
type: "POST",
url: '/BubbleStat.php',
cache: false,
dataType: 'json',
success: function(jsonData)
    {
        if (jsonData.total != null && jsonData.total != undefined)
        {                
            $("#Bubble_Msg").text(jsonData.total).show();
        } 
    } 
});

使用する変数を取得すると、問題はより簡単に見えます。

于 2012-12-25T17:49:44.887 に答える
1

のように... .text()?

$("#Bubble_Msg").text(html).show();

html-named 変数が実際に HTML を含む場合は、...代わり.html()に。

于 2012-12-25T17:13:58.230 に答える
1

このように使用します:

success: function(html)
{
    $("#Bubble_Msg").html(html).show(); 
} //-----------------------^^----------this html is the param passed in the 
  //-----------------------------------success function
于 2012-12-25T17:16:56.813 に答える
1

これを試して:

success: function(html) {

    // Check if the Counter have unread messages
    if (parseInt(html) > 0) {
        $("#Bubble_Msg").text(html).show();
    }
    else {
        $("#Bubble_Msg").hide();
    }
}​
于 2012-12-25T17:32:36.587 に答える