title: Passing Mysql data to PHP array then to javascript
body: <p>Let me start by explaining the situation:</p>
  
  <p>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.</p>
  
  <p>My JavaScript array should end up looking like this : </p>
  
  <p>first row:</p>
  
  <blockquote>
    <p><code>[laptime1 user1, laptime2 user1, laptime3 user1,...]</code></p>
  </blockquote>
  
  <p>second row:</p>
  
  <blockquote>
    <p><code>[laptime1 user2, laptime2 user2, laptime3 user2,...]</code></p>
  </blockquote>
  
  <p>Here's my current situation:</p>
  
  <p>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.<br>
  For example:<br>
  The first value was "8" instead of "84,521", then the second value was "4", etc..)... </p>
  
  <p>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.</p>
  
  <p>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) :</p>
  
  <pre><code>$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 =&gt; $value){
      if ($key &lt; (count($data)-1)){
          $script = $script . $value . ',';
      }
      else {
          $script = $script . $value . ");\n";
      }
  }
  </code></pre>
  
  <p>This outputs an array in JavaScript that looks like this :</p>
  
  <pre><code>myArray=[84.521,83.800,81.900]
  </code></pre>
  
  <p>Which is great, as this is exactly what my java script requires as input (time in seconds, separated by commas for each lap).</p>
  
  <p>Now I would like to implement the multiple user element but I'm stumped as to how I can work that out...</p>
  
  <p>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".</p>
  
  <p>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 ?</p>
  
  <p>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).</p>
  
  <p>Any help would be very much appreciated, thanks in advance !</p>