Вопрос

Let me start by explaining the situation:

I have a MySql table that contains several columns, of which a user id, a race id, a lap time, a lap number and I want to put this information into an array in PHP which I will then send to a java script.

My JavaScript array should end up looking like this :

first row:

[laptime1 user1, laptime2 user1, laptime3 user1,...]

second row:

[laptime1 user2, laptime2 user2, laptime3 user2,...]

Here's my current situation:

I first tried to test this situation for a single user and ran into lots of problems because my lap times in MySql are floats and when using json_encode it turned everything into strings, which did not work for my javascript as it started outputting the wrong values.
For example:
The first value was "8" instead of "84,521", then the second value was "4", etc..)...

Sadly, I found a potential solution with the numeric check option, but cannot use it as my hosting runs a PHP version that doesn't support it.

So I found the following solution, which I fiddled with a bit and that works for a single user (it might look messy to you, I'm really a beginner and punching above my weight, but it works) :

$query = doquery("SELECT racelaptime,userid FROM {{table}} WHERE raceid='1' ORDER BY racelap", "liverace");
while(($result = mysql_fetch_array($query))) {
    $data[] = (float)$result['racelaptime'];
}

$script = $script . "\nvar myArray = new Array(";
foreach ($data as $key => $value){
    if ($key < (count($data)-1)){
        $script = $script . $value . ',';
    }
    else {
        $script = $script . $value . ");\n";
    }
}

This outputs an array in JavaScript that looks like this :

myArray=[84.521,83.800,81.900]

Which is great, as this is exactly what my java script requires as input (time in seconds, separated by commas for each lap).

Now I would like to implement the multiple user element but I'm stumped as to how I can work that out...

My MySQL query is still sorted by race lap but I also kind of need to sort the data by user id as I want all the laps of each user sorted in 1 row, Also, the user id is unknown to me and can vary (depends which user posts the time) so I can't really do a "if userid==1 save this here and then go to next one".

Should I use a foreach statement in the while loop that stores the data, but how can I tell him to store all the laps by the same user in the first row (and the next user in the second row, etc...) without using tons of SQL queries ?

If you can offer a more elegant solution than my current one for passing the PHP array to JavaScript, I would be more than happy to make changes but otherwise a simple solution using the current "setup" would be great too (hope it's all clear enough).

Any help would be very much appreciated, thanks in advance !

Это было полезно?

Решение

For multiple user element I would use a multidimensional array >

$query = doquery("SELECT racelaptime,userid FROM {{table}} WHERE raceid='1' ORDER BY racelap", "liverace");

// Loop the DB result
while(($result = mysql_fetch_array($query))) {

    // Check if this ID is already in the data array
    if(!array_key_exists($result['userid'], $data)){

        // Create array for current user
        $data[$result['userid']] = array();

    }

    // Add the current race time to the array (do not need to use the float)
    $data[$result['userid']][] = $result['racelaptime'];

}

// Echo json data
echo json_encode($data);

Now what you need to do on the Javascript side when handling this array is to go through each of the user

$.each(data, function(key, value){

       // Use parseFloat to keep the decimal value
       alert(key + ' Has the following values - ' + value);

       // If you want to display the racing values you simply
       $.each(value, function(k, parseFloat(v)){

             alert(v);

       })

 })

Is this what you needed or am I completely out of the scope?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top