0

I have a website template which uses an external JAVSCRIPT file to plot data on to the graph. what I want to do is swap the demo content for php variables.

Original graph data

  var data = [{
  label: "Facebook",
  data: [[1, 2], [2, 37], [3, 12], [4, 72], [5, 24], [6, 113], [7, 27]]
  },
  {
  label: "Google+",
  data: [[1, 3], [2, 45], [3, 16], [4, 48], [5, 12], [6, 125], [7, 26]]
  }];

MY Attempt

 var data = [{
label: "Facebook",
 data: [[<?php echo '$daysago7'; ?>, 2], [<?php echo '$daysago6'; ?>, 37], [<?php echo   '$daysago5'; ?>, 12], [<?php echo '$daysago4'; ?>, 72], [<?php echo '$daysago3'; ?>, 24],   [<?php echo '$daysago2'; ?>, 113], [<?php echo '$daysago1'; ?>, 27]]
 },
 {
label: "Google+",
data: [[<?php echo '$daysago7'; ?>, 3], [<?php echo '$daysago6'; ?>, 45], [<?php echo '$daysago5'; ?>, 16], [<?php echo '$daysago4'; ?>, 48], [<?php echo '$daysago3'; ?>, 12], [<?php echo '$daysago2'; ?>, 125], [<?php echo '$daysago1'; ?>, 26]]

}]

When I include my version, i get no graph at all.

Charles
  • 50,943
  • 13
  • 104
  • 142
user1691024
  • 199
  • 3
  • 8
  • Today I stumbled across a very good answer to the question and [here it is](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work) – Havelock May 07 '13 at 19:18
  • My guess is that PHP isn't processing that file. Try to change the file to a `.php` file instead of a `.js` file. – gen_Eric May 07 '13 at 19:20
  • Are your variables getting set and is the server interpreting the php in your Javascript file? (what is the extension of the include you are trying to do). Seeing the structure of what you are trying to include/use might help answer your question. You are also missing a closing ; in your version. }] should be }]; – dougis May 07 '13 at 19:19

4 Answers4

1

For example, you can create a file javascript.php with this code:

<?php header("Content-type: text/javascript");?>
var data = [{

    }, { label: "Google+",  data: [[ < ?php echo '$daysago7'; ? >, 3], [ < ?php echo '$daysago6'; ? >, 45], [ < ?php echo '$daysago5'; ? >, 16], [ < ?php echo '$daysago4'; ? >, 48], [ < ?php echo '$daysago3'; ? >, 12], [ < ?php echo '$daysago2'; ? >, 125], [ < ?php echo '$daysago1'; ? >, 26]]}]
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
Emanuele Pavanello
  • 785
  • 1
  • 9
  • 22
1

Well, I use to do it like this:

PHP (Controller)

$dataPlot = [
    'label' => 'Facebook',
    'data' => [...]
];

View (PHP, Twig, dunno)

<div data-plot="<?php echo json_encode($dataPlot) ?>"></div>

JS (using jQuery)

$(function() {

    $('[data-plot]').each(function(i) {

        var div = $(this);
        var data = div.data('plot');

        div.someJQueryChartPlugin(data);

    });

});

CSS

[data-plot] {
    ...
}

This way you don't need to generate a JS with PHP (and send the right headers to make the browser understand what it is) and can add lots of charts easily.

coma
  • 16,429
  • 4
  • 51
  • 76
0

If you are writting php inside .js file it won't work. However you can do

<script language="JavaScript" type="text/javascript" src="/some_file.php"></script>

and make that .php file output javascript.

Goran Lepur
  • 594
  • 1
  • 4
  • 15
0

Just remove the apostrophes from your code.

Instead of

<?php echo '$daysago7';?>

use this

<?php echo $daysago7;?>

Apostrophes interprets $ sign as normal sign, not as php variable.

If you use php file for javascript as the other answer suggests, just add this header at the very top (before any whitespace) of the file:

<?php header("Content-type: text/javascript");?>

To help the client to determine its MIME type.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169