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?

Recommended Answers

All 5 Replies

<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

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.

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).

      $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)

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']'),

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.