0

I'm trying to assign a PHP array into a Javascript array, so I can use it into my chart.js file. I tried adding it into the javascript array but it didn't work, here is wat I have: This is my PHP gathering form for my data to be stored into the array:

$id = $_SESSION['userId']; 
      $dBname = "infosensor";
      $conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);

      $sql = "SELECT dias FROM `$id`;";
      $result = mysqli_query($conn, $sql);
      $resultCheck = mysqli_num_rows($result);

      if ($resultCheck > 0)
      {
        while ($row = mysqli_fetch_assoc($result))
        {
          // H:i:s -> Hora/minuto/segundo (H maior tem diferenca na forma de mostrar o horario, H maior -> 0h a 24h h menor -> 0h 12h)
          // d/m/Y -> dia/mes/ano
          $horario = (date('H:i:s', strtotime(str_replace('.', '-', $row['dias']))));    

          $tempo[] = explode(',', $horario);

        }
      }

The result that I get from printing the array $tempo is:

Valor: Array ( [0] => Array ( [0] => 16:29:47 ) [1] => Array ( [0] => 16:30:07 ) [2] => Array ( [0] => 16:33:55 ) [3] => Array ( [0] => 16:34:25 ) [4] => Array ( [0] => 16:34:41 ) [5] => Array ( [0] => 16:59:26 ) [6] => Array ( [0] => 16:59:36 ) [7] => Array ( [0] => 17:02:38 ) [8] => Array ( [0] => 17:05:55 ) [9] => Array ( [0] => 17:08:46 ) [10] => Array ( [0] => 17:08:47 ) [11] => Array ( [0] => 17:09:10 ) [12] => Array ( [0] => 17:45:25 ) [13] => Array ( [0] => 18:01:06 ) [14] => Array ( [0] => 18:05:55 ) [15] => Array ( [0] => 18:20:56 ) [16] => Array ( [0] => 19:26:19 ) [17] => Array ( [0] => 20:33:12 ) [18] => Array ( [0] => 20:33:17 ) [19] => Array ( [0] => 00:00:00 ) [20] => Array ( [0] => 09:17:48 ) [21] => Array ( [0] => 15:32:21 ) [22] => Array ( [0] => 15:40:18 ) [23] => Array ( [0] => 15:41:32 ) [24] => Array ( [0] => 23:51:15 ) [25] => Array ( [0] => 23:51:22 ) [26] => Array ( [0] => 23:56:30 ) [27] => Array ( [0] => 21:51:57 ) [28] => Array ( [0] => 21:52:18 ) [29] => Array ( [0] => 21:52:31 ) )

I need to store this datas like this: Obs: This is just an example of how I need to store it.

var date = ["20:01:10", "10:23:20", "23:50:20", "07:23:20"];
1
  • What have you tried so far to make it work? Additionally, be warned that your code is widely open to SQL injections Commented Mar 2, 2020 at 15:09

1 Answer 1

3

You need to use JSON to pass the array to javascript.

var date = <?php echo json_encode($yourArray) ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.