开发者

How to seperate the datas when jquery get them from mysql?

ChkNewRspLive.php

<?php
    $query3 = "SELECT msgid, id FROM rspnotificationlive WHERE username='{$username1}' ORDER BY id LIMI开发者_StackOverflow中文版T 99";
    $result3 = mysql_query($query3,$connection) or die (mysql_error());
    confirm_query($result3); 
    $numrspmsg = mysql_num_rows($result3);
    echo $numrspmsg . "|";
    while($userinfo3 = mysql_fetch_array($result3)){
        $rspmsgid= $userinfo3['msgid'];
        $msgid= $userinfo3['id'];
        echo $rspmsgid . ", ";
    }
?>

index.html

<script type="text/javascript">
$.get("ChkNewRspLive.php?username=" + username, function(newrspmsg){
        var mySplitResult = newrspmsg.split("|");
        var rspMsgCount = parseFloat(mySplitResult[0]);
        var rspMsgids =(mySplitResult[1]);
        var Msgids = ??//how to get this result from ChkNewRspLive.php ?
});
</script>

As you can see, I used "|" to separate $rspmsgid and $numrspmsg. I also use "," to separate multiple $rspmsgid. How if I want to separate another data $msgid? How to do that?

If I use | to separate $rspmsgid and $msgid, there will be many sign of | because they both are in the while loop.


JSON encode your content.

In your php, change your code to something like:

  $json = array();
    while($userinfo3 = mysql_fetch_array($result3)){
        $rspmsgid= $userinfo3['msgid'];
        $msgid= $userinfo3['id'];
        $json[] = array($rspmsgid,$msgid);
    }
  echo json_encode($json);

and then use $.getJson in your javascript.

You won't have to define the number of mysql_rows either, as you can just get that in javascript by using .length on the json data.

edit and escape your string before using it in your SQL!


You are already using the .split() method to seperate the other string. Apply it to the other part and let it split by ", " or just use another | instead of the , and you will have it split into three parts instead of two.

However I suggest you have a look at JSON. This should be the better solution if it gets more complicated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜