PHP data in Javascript document

0

Good afternoon.

I have the following PHP code, somewhat simple:

<?
    include("conexao.php");
    $sql = mysql_query("select top 1 * from grafico_tb order by data_referencia desc");
    $dados = mysql_fetch_assoc($sql);

    $dados['perc1'];
    $dados['perc2'];
?>

And I have the following code in javascript: grafico.js

initSparklineCharts: function() {
    if (!jQuery().sparkline) {
            return;
        }
    $("#sparkline_bar").sparkline([8, 9], {
                type: 'bar',
                width: '100',
                barWidth: 5,
                height: '55',
                barColor: '#f36a5b',
                negBarColor: '#e02222'
            });
};

Instead of "8" and "9", I intended to play variables created within the .php document. I have already tried to create this same graph inside the <script></script> tag in a .php as follows:

initSparklineCharts: function() {
    if (!jQuery().sparkline) {
            return;
        }
    $("#sparkline_bar").sparkline([<? $dados['perc1'].','.$dados['perc2']?>], {
                type: 'bar',
                width: '100',
                barWidth: 5,
                height: '55',
                barColor: '#f36a5b',
                negBarColor: '#e02222'
            });
};

And also with echo in front of variables but did not work. Any solution?

The graph works with the values "8" and "9" without being variables and the variables in php also work.

Thank you!

    
asked by anonymous 06.09.2016 / 22:54

3 answers

0

Test this here instead of your php

<?PHP print("$dados[perc1] , $dados[perc2]"); ?>

It will look like this

initSparklineCharts: function() {
    if (!jQuery().sparkline) {
            return;
        }
    $("#sparkline_bar").sparkline([<?PHP print("$dados[perc1] , $dados[perc2]"); ?>], {
                type: 'bar',
                width: '100',
                barWidth: 5,
                height: '55',
                barColor: '#f36a5b',
                negBarColor: '#e02222'
            });
};
    
07.09.2016 / 00:18
0

If you want to play PHP inside a JS file. You will create a PHP file and put it as an APLICATION / JAVASCRIPT output in a HEADER. It would look like this: grafico.php

    
07.09.2016 / 02:00
0

John I use a similar graphics plugin, but the application is the same in your model also follows my example:

[<?php
 $sql = "SELECT...";
 $result = $conn->query($sql);
 if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
        echo $row['data'].",";
  }
 ?>]
    
08.09.2016 / 21:01