Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

pass php array to javascript array

I'm looking for the best way to pass php array to javascript.

I'm making a RPG, and when they login, I'd like to return their saved data from database and store in an array on javascript side:

To get data, I do:

$.getJSON("php/CRUD.php", {"_functionToRun" : ""},
    function (returned_data) {
        game.data.dataArray.push(returned_data.split(" "));
        log("1: " + game.data.dataArray); //output: `1: 120,mymap2` 
        log("2: " + game.data.dataArray[0]); //output: `2: 120,mymap2`
    }
);

PHP:

    $qry = 
        'SELECT * 
        FROM userstats
        WHERE id_user_fk ="' . $_SESSION['userid'] . '"
        LIMIT 1';

    $result = $mysqli->query($qry) or die(mysqli_error($mysqli));

    while ($row = $result->fetch_assoc()) {
        $message =  $row['experience'] . $row['levelname'];
    }   

 echo json_encode($message);

1) Is this the best way to get a set of values into a js array?

2) Why can't I access a certain data element of game.data.dataArray using game.data.dataArray[0].

These give the same output of 1: 120,mymap2

        log("1: " + game.data.dataArray);
        log("2: " + game.data.dataArray[0]);

Should returned_data.split(" ") split the returned string into two array elements?


EDIT: Okay I've done echo json_encode($message); and it returns with quotes, but still returns same results for game.data.dataArray and game.data.dataArray[0]

1: "120,mymap2" 
2: "120,mymap2" 

I've also changed the function to $.getJSON


Changed again to

    $qry = 
        'SELECT * 
        FROM userstats
        WHERE id_user_fk ="' . $_SESSION['userid'] . '"';

    $result = $mysqli->query($qry) or die(mysqli_error($mysqli));

    while ($row = $result->fetch_assoc()) {
        $array[] =  $row['experience'] . $row['levelname'];
        die(json_encode($array[]));
    }   

and JS is:

$.getJSON("php/CRUD.php", {"_functionToRun" : ""},
    function (returned_data) {
        game.data.dataArray.push(returned_data.split(" "));
        log("1: " + game.data.dataArray);
        log("2: " + game.data.dataArray[0]);
    }
);

Outputs:

Uncaught melonJS: level <br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-...<omitted>...nd 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need to do is, change this code:

while ($row = $result->fetch_assoc()) {
    $message =  $row['experience'] . $row['levelname'];
}

To make it as an array:

while ($row = $result->fetch_assoc()) {
    $message[] =  $row['experience'] . $row['levelname'];
}

So, $message now becomes an array. You need to output this to the client. You can do that using:

die(json_encode($message));

The reason is, in your code, it will output only the last one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...