1

I'm building a hi score board in construct2 and am trying to lay out the chars correctly.

In order to do this I have created an array in c2 and am using AJAX to return data as a JSON string from a php script held on my webserver. This data will then be used to populate the array which I will then use to fill out my SpriteFonts. Simple enough in theory.

This is the php file that returns the JSON string:

if ($result = $mysqli->query($sql))  {
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
    $array = array(
        array
        (
            'name' =>  $row['pName'],
            'score' => $row['score']
        ));     
        echo json_encode($array);
    }

    /* free result set */
    $result->free();
    exit();
}

This returns the following:

[{"name":"developer","score":"56"}]
[{"name":"Terrry","score":"34"}]
[{"name":"Numero_Uno","score":"20"}]
[{"name":"Thomasin :)","score":"18"}]
[{"name":"ThriftyButStillNifty","score":"18"}]
[{"name":"Perfect","score":"17"}]
[{"name":"bah","score":"17"}]
[{"name":"EvilEdna","score":"16"}]
[{"name":"type here","score":"16"}]
[{"name":"Slaine","score":"14"}]

To be used in C2 the string needs to be in this format:

{"c2array":true,
"size":
    [2,2,1],
"data":
[
[["John"],[23]],
[["Terry"],[43]]
]
}

How do I go about creating one from t'other?

1 Answer 1

1

I would do something like this:

$response = array(
    "c2array" => true,
    "size" => "something",
    "data" => array()
);

while ( $row = $result->fetch_assoc() ) {
    $response['data'][] = array(
        array($row['pName']),
        array($row['score'])
    );
}

echo json_encode($response);
Sign up to request clarification or add additional context in comments.

1 Comment

Ye, thats perfect with the slight addition of $foo = array(10,2,1); to cater the size section of the response. TVM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.