0

I have been avoiding javascript for a while now but need to use it for a google chart I am using. My original code looks like this...

echo "function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'Machines Total'  ],

      [ '$Day[6]', $Hour_Tot[7].$Min_Tot[7] ], 
      [ '$Day[5]', $Hour_Tot[6].$Min_Tot[6] ], 
      [ '$Day[4]', $Hour_Tot[5].$Min_Tot[5] ],
      [ '$Day[3]', $Hour_Tot[4].$Min_Tot[4] ],
      [ '$Day[2]', $Hour_Tot[3].$Min_Tot[3] ],
      [ '$Day[1]', $Hour_Tot[2].$Min_Tot[2] ],
      [ '$Day[0]', $Hour_Tot[1].$Min_Tot[1] ]

      ]);";

This code works just fine, and has been tested. What I am trying to do now is have the chart become more dynamic so a user can enter in a number and the chart can ouput the data for the number of days. So I need to add in a for loop. This is what I got so far.

echo "function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'Machine L1'  ],
      for (count = 1, DayNumber = 0; count == 7; ++count, ++DayNumber ) 
  document.write([ '$Day[DayNumber]', $Hour_Tot[count].$Min_Tot[count]],);
       ]);";

This code does not work. I do not understand how to use the document.write to output the 7 lines I need to replicate the above code.

6
  • 1
    Why dont you use PHP for that iteration? Commented Jun 19, 2013 at 20:04
  • what @NULL said. echoing javascript from php like that is crazy, man. Commented Jun 19, 2013 at 20:09
  • google charts is echoing the javascript in the php. I am just using there format. Commented Jun 19, 2013 at 20:13
  • I apologize I was the one who echoed the javascript like that. I will see if i can change that up as well. Commented Jun 19, 2013 at 20:21
  • 1
    try writing your javascript functions in a separate file, then echoing just the data from php to a javascript variable, which you can then use in a function. that way you can keep js and php entirely separate. Commented Jun 19, 2013 at 20:23

1 Answer 1

1

This code is not going to work because you're putting a for loop inside an array declaration. You should do the following...

echo "function drawChart() {
var data = google.visualization.arrayToDataTable([
  ['Date', 'Machine L1'  ],";
for ($count = 1, $DayNumber = 0; $count <= 7; ++$count, ++$DayNumber ) {
  echo "  [ '$Day[$DayNumber]', $Hour_Tot[$count].$Min_Tot[$count]],";
}
echo "]);";

Or you can also handle that using javascript, it should looks like:

echo "var arr = [['Date', 'Machine L1']];
  for(var i=1;i<=7;i++) {
    arr.push([{$Day}[i-1], {$Hour_Tot}[i].toString()+{$Min_Tot}[i].toString()]);
  }
  var data = google.visualization.arrayToDataTable(arr);
";
Sign up to request clarification or add additional context in comments.

1 Comment

PERFECT!! Thanks for the quick answers. Did not work at first, but I added <=. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.