Click here to Skip to main content
15,117,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I hope you are well I am new to this platform. I need some help.
I have 3 files: an HTML, PHP and JS file. In my PHP file, I have a variable of type array() containing 5 values, and this variable will be used to feed a graph. However I don't know how to reuse my variable in my PHP code to my JS code.

I thank you in advance for the answers you will bring me.

What I have tried:

Here is the variable in my PHP code:
PHP
$dataSignal = array($lqi_excellent, $lqi_good, $lqi_average, $lqi_limit, $lqi_na);


Here is my js code :
JavaScript
new Chart(document.getElementById("doughnut-chart"), {
  type: 'doughnut',
  data: {
    labels: ["Excellent", "Good", "Average", "Limit", "N/A"],
    datasets: [
    {
      backgroundColor: ["#27AE60", "#3498DB", "#F1C40F", "#E74C3C", "#17202A"],
      data: [1,2,3,4,5] --> this is where I want to reuse my variable
    }
    ]
  },
  options: {
    plugins: {
      legend: {
        title: {
          display: true,
          text: 'Signal quality'
        }
      }
    }
  }
});
Posted
Updated 17-Oct-21 23:17pm

1 solution

Since PHP runs on the server, you need to add the code into the page that will be sent to the client. Something like:
PHP
    datasets: [
    {
      backgroundColor: ["#27AE60", "#3498DB", "#F1C40F", "#E74C3C", "#17202A"],
<?php
      echo "data: [ " . $lqi_excellent . ", " . $lqi_good . ", " . $lqi_average . ", " . $lqi_limit . ", " . $lqi_na . " ]" 

?>
    }
    ]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900