0

I'm trying to add a graph to my data. This data is imported from phpmyadmin. The data goes through, i have the tables under it. When I try to add the data, I get an error because it translates my php to [<br />.

<script type='text/javascript'>
var ctx = document.getElementById('myChart').getContext('2d');

var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: [<?php echo $Timers; ?>],
        datasets: [{
            label: 'Time',
            data: [<?php echo $Wheights; ?>],

the php

while ($stelling = mysqli_fetch_array($records1)){

            $Timer = date('M, Y', strtotime($stelling['Timer']));
            $Wheight = $stelling['Wheight'];
            $Title = $stelling['Title'];

$Timers = '';
$Wheights = '';
$Titles = '';

$Timers = $Timers.'"'.$Timer.'",';
$Wheights = $Wheights.$Wheight.',';
$Titles = $Titles.$Title.',';

$Timers = trim($Timers, ",");
$Wheights = trim($Wheights, ",");
$Titles = trim($Titles, ",");



}include ("script/graph.php");
    }

This is the error:

var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: [<br />
< etc

I expect the data to come through the chart.js to form a graph Here is the database

Thomas
  • 61
  • 1
  • 10
  • 1
    Seems that `$Timers` is not defined and you are getting a notice – ka_lin Jun 04 '19 at 13:50
  • @ka_lin Thanks, yeh, its outside the function where it gets defined. I now tried to include it right before the bracket, but then it doesn't show at all. I tried this: `$Timers = trim($Timers, ","); $Wheights = trim($Wheights, ","); $Titles = trim($Titles, ","); include ("script/graph.php"); }` – Thomas Jun 04 '19 at 13:56

2 Answers2

1

<br /> is part of php error, that doesn't show completely in rendered html.

if you want to see actual error, you can see page source (Ctrl+u).

yaya
  • 7,675
  • 1
  • 39
  • 38
  • this is what the console is saying: Uncaught SyntaxError: Invalid or unexpected token (refering to the <) – Thomas Jun 04 '19 at 14:36
  • @Thomas no, not the console. see the html of result. php error is commented out. – yaya Jun 04 '19 at 14:48
  • yes, you mean where there is the red cross in the source code of the html? because that says the same thing – Thomas Jun 04 '19 at 14:53
  • @Thomas no, "Invalid or unexpected token" is js error. find php error in html source. – yaya Jun 04 '19 at 15:05
  • I think that's why I also don't get out of this problem. The php doesn't show any errors, Because my php errors should show automatically. At least, it has for my whole time working on this file. @yaya pro – Thomas Jun 05 '19 at 09:57
  • @Thomas you should isolate your problem. remove all of your codes, just write `` and see what will render. if nothing, usethis to enable php error reporting : https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – yaya Jun 05 '19 at 10:07
  • Yes, I have done so. I added this line to the php and it prints it prints out: "Apr, 2019""Apr, 2019" as expected. The problem is that javascript does not recognize the variable although i included it in the function. – Thomas Jun 05 '19 at 11:33
  • @Thomas i saw your updated question. you should change it back to . you should share your full code. where is js located? in a .php file? how these two files connected to each other? currenlt your question is vague. – yaya Jun 05 '19 at 12:17
  • sorry, I rolled it back. I'm just running through a series of problems trying to show the data from the database to chart.js. But my question is answered. I no longer have the – Thomas Jun 05 '19 at 13:40
1

It seems issue at your javascript code..it is not converting the php array to javascript properly.

try this:

<script type='text/javascript'>
var ctx = document.getElementById('myChart').getContext('2d');
var labels = '<?php echo json_encode($Timers); ?>';

var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: labels,
        datasets: [{
            label: 'Time',
            data: [<?php echo $Wheights; ?>],
Vikash Pathak
  • 3,444
  • 1
  • 18
  • 32