0

I am using Google Analytics API to pull Total Visits and put into Googles Javascript to display in a bar graph. I am having issues trying to put an array to the variables. When I do a print_r it spits out everything just fine but I need it to spit out everything just as it is. This is what I got so far:

//I connect to my own SQL database for $num

//Connect to Google Analytics
$mystart = '2012-02-28';

$i=0;
while ($i < $num) {

        $ga->requestReportData(ga_profile_id,array('source'),array('visits', 'visitBounceRate'),'-visits', $filter, $mystart, $mystart);

//adds +1 day to date 
$mystart = date('Y-m-d', strtotime("+1 day", strtotime($mystart)));

$totalvisits = $ga->getVisits();

//These are my arrays  
$adddate[] = "data.addColumn('number', '" . $mystart . "');";
$addvisits[] = "$totalvisits,";
        }

This is what I am trying to achieve through the use of echos:

<script type="text/javascript">
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        // This is where I want to put something like <? echo $adddate ?> but of course doesn't work
        data.addColumn('number', '2012-02-28');
        data.addColumn('number', '2012-02-29');
        data.addColumn('number', '2012-03-01');
        data.addColumn('number', '2012-03-02');
        data.addColumn('number', '2012-03-03');
        data.addRows([
        // This is where I want to put something like <? echo $addvisits ?> but of course doesn't work
          ['Feb. 28 to March 3', 100, 105, 91, 80, 150]
        ]);
</script>
1
  • 1
    Why aren't you using JSON? Commented Mar 5, 2012 at 15:44

4 Answers 4

3

If you are asking how to output the array in the way you want, then use something like:

echo implode("\r\n", $adddate);

See implodeDocs.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. The implode is key of course, thanks again.
1

I suggest making an array, and then echoing it as JSON, and having JS loop over it.

For example:

$adddate[] = array('type' => 'number', 'value' => $mystart);

And then:

var addDate = <?php echo json_encode($adddate); ?>;
for(var i = 0, len = addDate.length; i < len; i++){
   var val = addDate[i];
   data.addColumn(val.number, val.value);
}

Then you can do something similar for $addvisits.

Comments

0

Of course, it will work, you cannot echo an array. The easiest way is to implode them

Use these codes, where you are echoing them right now

echo implode("",$adddate);
echo implode("",$addvisits);
// User \r\n to implode if you need to add linebreaks

Comments

-2

Why is it an array, when there is only one value? use simply this:

$adddate = "data.addColumn('number', '" . $mystart . "');";

(remove the [])

and then the <? echo $adddate ?> will work

1 Comment

What if he wanted to add more values? Note $adddate[] is in a while loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.