1,105,281 Community Members

Google Chart

Member Avatar
davy_yg
Veteran Poster
1,114 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 1 [?]
 
0
 

Hello I am trying to create a chart based on clicks information.

How to do so ?

// print jumlah klik dalam graphic


$result = mysql_query("SELECT COUNT(idads) AS countidads FROM adsmgt") or die(mysql_error()); 

$resultday = mysql_query("SELECT COUNT(idads) AS countidadsday FROM adsmgt GROUP BY date") or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
echo "Number of ad clicks: ".$row['countidads'];

}

$result2 = mysql_query("SELECT waktu, date, time FROM adsmgt") or die(mysql_error());

echo "<br>";
echo "<br>"."Tanggal"."&nbsp&nbsp&nbsp"."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaktu"."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJumlah clicks per hari";

while($row2 = mysql_fetch_array($result2))
{
echo "<br>".$row2['date']."&nbsp&nbsp&nbsp".$row2['time']."&nbsp&nbsp&nbsp".$row['countidadsday'];
}

while($rowday= mysql_fetch_array($resultday))
{
echo $rowday['countidadsday'];
}

//php_info();

// Graph - http://www.ebrueggeman.com/phpgraphlib/examples

?>

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['date', 'Number of clicks per hari'],
          ['18',  10,     ],
          ['19',  11,     ],
          ['20',  8,      ],
          ['21',  5,      ]
        ]);

        var options = {
          title: 'Company Performance',
          hAxis: {title: 'Date',  titleTextStyle: {color: 'red'}}
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

This is what I have so far. Just one problem - I am using Google Chart. The information is not updated since I have to enter it manually to the graph and is not automatic. I would like it to be automatics based on the ads click. Is that posible?

Member Avatar
Biiim
Posting Pro
504 posts since Oct 2011
Reputation Points: 85 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 9 [?]
 
0
 
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['date', 'Number of clicks per hari'],
          <?php

          $data = array(
                '1'=>array('date'=>'2013-01-16','numclicks'=>'6'),
                '2'=>array('date'=>'2013-01-17','numclicks'=>'9'),
                '3'=>array('date'=>'2013-01-18','numclicks'=>'5'),
                '4'=>array('date'=>'2013-01-19','numclicks'=>'12'),
                '5'=>array('date'=>'2013-01-20','numclicks'=>'10'),
            );
            //$resultday = mysql_query("SELECT `date`,COUNT(idads) AS `numclicks` FROM adsmgt GROUP BY date") or die(mysql_error());

            $i = 0;
            //while($row = mysql_fetch_assoc($resultday)){
            foreach($data as $row){
                if($i == 0){
                    echo "['{$row['date']}', {$row['numclicks']}]";
                }else{
                    echo ",['{$row['date']}', {$row['numclicks']}]";
                }
                $i++;
            }
            ?>
        ]);
        var options = {
          title: 'Company Performance',
          hAxis: {title: 'Date',  titleTextStyle: {color: 'red'}}
        };
        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

Tested the hard coded array and worked, I put in what should be the code for your mysql query to work just need to uncomment and comment the foreach() line instead.

I had a play with the google graphs like 6 months back so have a little familiarity, they are very nice just very complex javascript

Member Avatar
davy_yg
Veteran Poster
1,114 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 1 [?]
 
0
 

Getting close. It just I still have fill in the information in the array to draw the graph. Unable to draw graph directly from $row['countidadsday'] (number of clicks for each specified date.

Member Avatar
davy_yg
Veteran Poster
1,114 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 1 [?]
 
0
 

Is it possible to draw graph directly from mysql_query information? ( I do not want to input the information manually it has to be automatic).

Member Avatar
jstfsklh211
Junior Poster
155 posts since Apr 2011
Reputation Points: 38 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 2 [?]
 
0
 
      $data = array(
            '1'=>array('date'=>'2013-01-16','numclicks'=>'6'),
            '2'=>array('date'=>'2013-01-17','numclicks'=>'9'),
            '3'=>array('date'=>'2013-01-18','numclicks'=>'5'),
            '4'=>array('date'=>'2013-01-19','numclicks'=>'12'),
            '5'=>array('date'=>'2013-01-20','numclicks'=>'10'),
        );

(replace this array with info from your query)

Member Avatar
davy_yg
Veteran Poster
1,114 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 1 [?]
 
0
 

Hello,

I try this:

<html>
    <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
    var data = google.visualization.arrayToDataTable([
    ['date', 'Number of clicks per hari'],
    <?php

    $result2 = mysql_query("SELECT waktu, date, time FROM adsmgt") or die(mysql_error());

    echo "<br>";
    echo "<br>"."Tanggal"."&nbsp&nbsp&nbsp"."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWaktu"."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJumlah clicks per hari";

    while($row2 = mysql_fetch_array($result2))
    {
    echo "<br>".$row2['date']."&nbsp&nbsp&nbsp".$row2['time']."&nbsp&nbsp&nbsp".$row['countidadsday'];
    }

    $data = array(
    '1'=>array('date'=>'$row2['date']','$row['countidadsday']'),
    '2'=>array('date'=>'$row2['date']','$row['countidadsday']'),
    '3'=>array('date'=>'$row2['date']','$row['countidadsday']'),
    '4'=>array('date'=>'$row2['date']','$row['countidadsday']'),
    '5'=>array('date'=>'$row2['date']','$row['countidadsday']'),
    );

Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\xampp\htdocs\Innovation\script_shop\adsmgt2.php on line 87

line 87: '1'=>array('date'=>'$row2['date']','$row['countidadsday']'),

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article