I got problems with making my php array to a js array to use it to build a chart.
code: index.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<script type="text/javascript" src="js/chart.js"> var percentage = <?php echo json_encode($array_percentage); ?>;</script>
</head>
<body>
<div class="ct-chart ct-perfect-fourth"></div>
</body>
</html>
code chart.js:
console.log(percentage);
Could really use some help ^^
This line is wrong:
<script type="text/javascript" src="js/chart.js"> var percentage = <?php echo json_encode($array_percentage); ?>;
</script>
Should be:
<script> var percentage = <?php echo json_encode($array_percentage); ?>;</script>
Plus, json_encode will ouput an object, not an array.
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
Gives you:
{"a":1,"b":2,"c":3,"d":4,"e":5}
that’s because i need to link it to my external JS file for my chart…
Then:
<script type="text/javascript" src="js/chart.js"></script>
<script> var percentage = <?php echo json_encode($array_percentage); ?>;</script>
Or I’ve totally missed what you’re trying to do.
system
Closed
5
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.