0

I am using jqplot and have constructed an array with the following PHP code

if($fiveRes){
    $lastNum = mysql_num_rows($fiveRes);

    $testarray = array();

    while($row = mysql_fetch_array($fiveRes, MYSQL_ASSOC)) {
        $testarray[] = array($row['GameDate'],  floatval($row['WKP']));
    }

    echo json_encode($testarray);

}

This code outputs the correct code that I need to insert into the jqplot function. Here is the array generated by the code above:

[["2011-12-24",0],["2011-12-19",14],["2011-12-08",22],["2011-12-04",14],["2011-11-27",12]]

So currently i'm printing this array to the screen, and then using jQuery .text() to capture the string and place it inside a variable. I can echo out the var that I assigned the array string to and it works correctly, however when I pass it into the jqplot function it does nothing.

var p1array = $('.col.first .parray').text();

alert(p1array); //Alerts the correct array formatted like above.

    var plot1 = $.jqplot('jqplot0', [p1array], {
          title:'Last 5 Fantasy Points',
          axes:{
            xaxis:{
              renderer:$.jqplot.DateAxisRenderer,
              tickOptions:{
                formatString:'%b %#d'
              } 
            },
            yaxis:{
              tickOptions:{
                formatString:''
                }
            }
          },
          highlighter: {
            show: true,
            sizeAdjust: 7.5
          },
          cursor: {
            show: false
          }
      });

To make it even more complicated, if I copy the string that the PHP generates and I hardcode it into a variable in JS it works. Any ideas why the jqplot plugin won't evaluate the string I get from using $(this).text();.

Here is the jQplot example i'm modeling after: http://www.jqplot.com/tests/cursor-highlighter.php

jsheffers
  • 1,602
  • 19
  • 41

2 Answers2

0

Using this method helped me solve this problem. Instead of using jQuery to access the printed variable I inserted a little bit of javascript at the bottom of my PHP page to capture the local variable in PHP and then used that to pass into the jQplot function.

Pass a PHP string to a JavaScript variable (and escape newlines)

Community
  • 1
  • 1
jsheffers
  • 1,602
  • 19
  • 41
0

Use [p1array] directly with out braces and try... it should work just like p1array

Anand thakkar
  • 479
  • 3
  • 8