-1

I'm using chartist to draw a chart. Now the points to put on the chart come as an array and i'm looping over all results to draw multiple charts.

According to the current place in the loop I want a chart with the values from the PHP array.

This is my current code

$('.ct-chart-current').each(function(i,v) {
    new Chartist.Line(this, {
        labels: [<?php echo $labelscurrent[0] ?>],
        series: [
            [<?php echo $current[X] ?>]
        ]
    }, currentopts);
});

You can see I'm trying to get value X from the $current array. Now how could I use the "i" from the function call in my each loop

Pseudo code

$('.ct-chart-current').each(function(--> i <-- ,v) {
    new Chartist.Line(this, {
        labels: [<?php echo $labelscurrent[0] ?>],
        series: [
            [<?php echo $current[ -->i <--] ?>]
        ]
    }, currentopts);
});

I added arrows around the javascript "i" I would love to echo in my PHP. I've tried different kind of method's like echo'ing <script> tags in my PHP but I'm guessing there should be an easier way?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable – Hamza Zafeer Mar 11 '16 at 13:22
  • I've seen this thread, It's not the same situation, since I'm already spitting out PHP in javascript. And I would need to echo Javascript in PHP that is sitting in Javascript. Hope you understand. – Miguel Stevens Mar 11 '16 at 13:23
  • PHP runs before JS, so what you're asking for wouldn't be possible. Perhaps you make an ajax call to the PHP with the value of `i` and have it return what you need. – Waxi Mar 11 '16 at 13:24

2 Answers2

4

Not an easier one. You just need to understand the MAIN difference between PHP and Javascript. The first one is server-side. Second is client-side. When client got the javascript, server-side (so, PHP) was already parsed.

A way to perform what you want is to output the $current array using a simple format as JSON, which is easy to grab in Javascript. For example...

var currentArray = <?php echo json_encode($currentArray); ?>;
var labels = <?php echo json_encode($labelscurrent); ?>;
$('.ct-chart-current').each(function(i, v) {
    new Chartist.Line(this, {
        labels: labels[i],
        series: [currentArray[i]]
    });
}, currentopts);

You have to replace variables by yours, obviously, because we miss some pieces of your code ;) But you have the trick : pass values from PHP to Javascript by echoing json_encoded values.

Lpu8er
  • 227
  • 1
  • 5
1

You can JSON encode your PHP variables and print them.
For your series data you need to put your data into a separate JavaScript variable and then access it when JavaScript runs.

var series_data = <?php echo json_encode($current); ?>;
$('.ct-chart-current').each(function(i,v) {
    new Chartist.Line(this, {
        labels: <?php echo json_encode($labelscurrent[0]); ?>,
        series: [
            series_data[i]
        ]
    }, currentopts);
});
gre_gor
  • 6,669
  • 9
  • 47
  • 52