Variable in php for javascript

0

I'm using HighChart to create graphics, but I'm not able to pass the value of data through a php variable:

series: [{
      name: 'Tendência',
      data: <?php echo $encodeValorTendencia; ?>
   },{
      name: 'Obtenção',
      data: <?php echo $encodeValorObtencao; ?>
}],

I'm already "encodando" the variables:

$valorTendencia[] = $value->Valor;
$encodeValorTendencia = json_encode($valorTendencia);

$valorObtencao[] = $value->ValorObtencao;
$encodeValorObtencao = json_encode($valorObtencao);

$dataHora[] = $value->DataHoraPrevisao;
$encodeDataHora = json_encode($dataHora);

The value of the variable $dataHora works through this section:

xAxis: {
    categories: <?php echo $encodeDataHora; ?>
},

In this way, the graph appears with the date and time but not the series values.

    
asked by anonymous 25.09.2017 / 18:10

1 answer

1

To solve, I used the constant JSON_NUMERIC_CHECK , of the function json_encode , thus:

$valorTendencia[] = $value->Valor;
$encodeValorTendencia = json_encode($valorTendencia, JSON_NUMERIC_CHECK);

$valorObtencao[] = $value->ValorObtencao;
$encodeValorObtencao = json_encode($valorObtencao, JSON_NUMERIC_CHECK);

And by going directly to JavaScript:

series: [{
   name: 'Tendência',
   data: <?php echo $encodeValorObtencao; ?>
},

The values even though they were numbers, were as string ("value"): string '["2000","2000","2000","700","800","700","2000","2000","2000‌​","2000"]'

Using the constant JSON_NUMERIC_CHECK , the values become numeric, since the api requires that the values passed in data: be numeric.

:string '[2000,2000,2000,700,800,700,2000,2000,2000,2000]'
    
25.09.2017 / 18:31