获取正确的num_rows值返回AJAX / JSON

时间:2015-11-05 21:00:57

标签: php json ajax mysqli polling

我有一个通知系统,当用户在我的墙上贴一个时,输出正确的值为1并相应地更新div。

<xsl:for-each select="/*/hundreds/hundred">
  <xsl:variable name="h" select="."/>
    <div class="page_spacer" />
    <div class="page_section" style="{./style}">
    <h2><xsl:value-of select="./label"/></h2>
    <xsl:value-of select="./descriptiontext" />
    <button onclick="javascript:window.location.href='{./viewalllink}'"><xsl:value-of select="./label" /></button>
    <br/>
    <xsl:for-each select="./contendors/contendor">
        <img class="locationthumb" src="/act/locationthumb/{$h/locid}"/>
    </xsl:for-each>
    </div>
    </xsl:for-each>

但是如果用户发布了两次,则它什么都不做,根本不更新。

{
    "num":1,
    "notification_id":"640",
    "notification_content":"Lucy  Botham posted a status on your wall",
    "notification_throughurl":"singlepoststreamitem.php?streamitem_id=515",
    "notification_triggeredby":"85",
    "notification_status":"1"
}

客户端

{
    "num":1,
    "notification_id":"641",
    "notification_content":"Lucy  Botham posted a status on your wall",
    "notification_throughurl":"singlepoststreamitem.php?streamitem_id=516",
    "notification_triggeredby":"85",
    "notification_status":"1"
}
{
    "num":1,
    "notification_id":"642",
    "notification_content":"Lucy  Botham posted a status on your wall",
    "notification_throughurl":"singlepoststreamitem.php?streamitem_id=517",
    "notification_triggeredby":"85",
    "notification_status":"1"
}

服务器端

$user1_id=mysqli_real_escape_string($mysqli,$_SESSION['id']);
$call="select MAX(notification_id) AS notification_id ,notification_status,notification_targetuser,notification_triggeredby,notification_throughurl from notifications WHERE notification_targetuser='$user1_id' AND notification_status='1'";
$chant=mysqli_query($mysqli,$call) or die(mysqli_error($mysqli));


while($notification=mysqli_fetch_array($chant)){
?>
<script type="text/javascript">

var notification_id="<?php echo $notification['notification_id']?>";
var notification_targetuser="<?php echo $notification['notification_targetuser']?>";
var notification_triggeredby="<?php echo $notification['notification_triggeredby']?>";
function loadIt() {

$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id+"&notification_targetuser="+notification_targetuser+"&notification_triggeredby="+notification_triggeredby,   
dataType:"json",
success: function(data){
if(data.notification_stream=1){
 $("#notif_ui"+notification_id).prepend('<div class="notif_text"><div id="notif_actual_text-" class="notif_actual_text"><img border="1" src="userimages/cropped'+data['notification_triggeredby']+'.jpg" onerror=this.src="userimages/no_profile_img.jpeg" width="40" height="40" ><br /><a href="'+data['notification_throughurl']+'">'+data['notification_content']+' </a><br />'+data['notification_time']+'<br/></div></div></div><hr/>');

 i = parseInt($("#mes").text()); $("#mes").text((i+data.num)); 

    if(!data.notification_id.length) {
   //no results...
   return;
}
notification_id = data.notification_id; 
}
}
});
}
setInterval(loadIt, 10000);   
 </script>
<? } ?>

1 个答案:

答案 0 :(得分:1)

您在循环内回显JSON结果,因此如果循环执行多次,则最终结果不是有效的JSON数据。它只是JSON的多个块。

while($row = mysqli_fetch_assoc($com)){
    $id = $row['notification_id'];
    $num = mysqli_num_rows($com);
    if($num){
        $json['num'] = 1;
    }else{
        $json['num'] = 0;
    }
    $json[$id]['notification_id'] = $row['notification_id'];
    $json[$id]['notification_content'] = $row['notification_content'];
    $json[$id]['notification_throughurl'] = $row['notification_throughurl'];
    $json[$id]['notification_triggeredby'] = $row['notification_triggeredby'];
    $json[$id]['notification_status'] = $row['notification_status'];
}
echo json_encode($json);

然后,您需要调整Javascript以期望数组而不是平面对象。