Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 !

share|improve this question
    
share your $data output and desire java script array format.. –  Neeraj Singh Mar 7 at 12:13
    
The desired JavaScript array format I have above is okay, however I simply wanted to have each row contain another user's lap times (same format though), that way I can parse through it in my script using myArray[lap number][user]. My problem was more on the php side, my $data is an array that simply contains the same times as the myArray example above (but in php). –  Staab Mar 7 at 12:47
    
if you are getting simple php array data then you should use '$jArray = implode (',', $data)'. it will concat your array data with comma separated like (11221.45, 1212.66, 21212.66) and then later you convert it in java script like <script>var jsArray = new Array ('<?php echo $jArray; ?>');</script>. let me know if it's not working. –  Neeraj Singh Mar 7 at 13:00
    
Will that take into account several rows though ? As in, will it concat each values per row together separated by commas or will it mesh it all within one row ? –  Staab Mar 7 at 13:03
    
yes, because you are storing only one column from each array data. it would be nice if you share your array data with print_r. or you can use group_concat and group by clause in your query statement. like, "SELECT user_id, group_concat( laptime ) AS laptime FROM speed GROUP BY user_id ORDER BY user_id LIMIT 0 , 30" –  Neeraj Singh Mar 7 at 13:09
show 3 more comments

1 Answer

up vote 0 down vote accepted

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?

share|improve this answer
    
The "check if this ID exists" is actually a great solution for my first problem, this way I should have a proper array and sorted per user and per lap as I wanted it, thanks I hadn't thought of that ! The Javascript part I'm not entirely sure yet, I will try and implement your suggestion, see how it's being output and come back to confirm. –  Staab Mar 7 at 12:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.