Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
1  
Why aren't you using JSON? –  Vyktor Mar 5 '12 at 15:44

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
This is exactly what I was looking for. The implode is key of course, thanks again. –  ToddN Mar 5 '12 at 15:50

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.

share|improve this answer

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
share|improve this answer

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

share|improve this answer
    
What if he wanted to add more values? Note $adddate[] is in a while loop. –  Rocket Hazmat Mar 5 '12 at 15:53
    
ok, sorry my mistake –  Jiří Herník Mar 5 '12 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.