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.

anyone can help me about this problem thank you in advance this is a google chart I need to change the task and Hours per day dynamically, so basically need a database that can connect to the database and will put them on array how can I do that.

 <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([
              ['Task', 'Hours per Day'],
              ['Work',     11],
              ['Eat',      2],
              ['Commute',  2],
              ['Watch TV', 2],
              ['Sleep',    7]
            ]);

            var options = {
              title: 'My Daily Activities'
            };

            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
        </script>
      </head>
      <body>
        <div id="chart_div" style="width: 900px; height: 500px;"></div>
      </body>
    </html>
share|improve this question
    
where is the php? you need php to connect to mysql database –  Ankur Sharma Apr 5 '13 at 8:33
    
I can connect to the database I only need how to create a array for javascript basically I need this code to change into dynamic: var data = google.visualization.arrayToDataTable([ ['Task', 'Hours per Day'], ['Work', 11], ['Eat', 2], ['Commute', 2], ['Watch TV', 2], ['Sleep', 7] ]); –  Ralph Ramil Zamora Apr 5 '13 at 8:37
add comment

2 Answers

up vote 3 down vote accepted

First you need to design a database structure. For example:

** Activities ** 
| ID |   Task   | Hours  | 
| 1  |   Work   |   11   |
| 2  |   Eat    |   2    |
| 3  | Cummute  |   2    |
| 4  | Watch TV |   2    | 
| 5  |  Sleep   |   7    | 

Now you need to make a database connection. ( easiest way, there a much better ways to do this, this is just an example )

<?php
 $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
 if (!$link) {
     die('Not connected : ' . mysql_error());
 } 

 // make foo the current db
 $db_selected = mysql_select_db('your_db_name', $link);
 if (!$db_selected) {
     die ('Can\'t use database : ' . mysql_error());
 }
?>

You need to build this code dynamic:

   var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

In this example, the title is static

   var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      <?php
         $query = mysql_query("SELECT * FROM Activities");
         $count = mysql_row_count($query);
         $i = 0;
         $last = ',';
         while ($row = mysql_fetch_array($result)) {
          $i++;
          if($i == $count) { $last = ''; )
          echo "['". $row['Task'] .", ". $row['Hours'] ." '] " . $last
         }
       ?>
    ]);
share|improve this answer
    
dont use mysql functions always use mysqli functions. –  Saurabh Sinha Apr 5 '13 at 8:44
    
@SaurabhSinha thats why I say in it "Now you need to make a database connection. ( easiest way )" I'm not suggesting its the best way. if you want to do it the best way use PDO... –  S.Visser Apr 5 '13 at 8:45
    
And is that why you downvote me? –  S.Visser Apr 5 '13 at 8:45
    
you need not have $count = mysql_row_count($query); and then check that if condition for the count. the while keyword will only run for the rows returning true. –  Saurabh Sinha Apr 5 '13 at 8:47
    
if($i == $count) { $last = ','; ) this line will produce an error. why do we need to have that last paranthesis –  Saurabh Sinha Apr 5 '13 at 8:48
show 5 more comments

try the following:

  1. Make DB Connect with mysqli_connect and query the DB for the resultset
  2. Assign the PHP variable as mentioned below:

              ['Task', 'Hours per Day'],
              ['Work',     <?php echo $work; ?>],
              ['Eat',      <?php echo $eat; ?>],
              ['Commute',  <?php echo $commute; ?>],
              ['Watch TV', <?php echo $watchTv; ?>],
              ['Sleep',    <?php echo $sleep; ?>]
    

The variables :

$work
$eat
$commute
$watchTv
$sleep

are the PHP variables which contains corresponding values from DB

I hope this would help.

share|improve this answer
    
Not really dynamic –  S.Visser Apr 5 '13 at 8:35
    
@S.Visser: why not dynamic, could you please explain –  Saurabh Sinha Apr 5 '13 at 8:36
    
I need the "Task" and "Hours per day" to be dynamic and it comes to the database anyone can update the code. –  Ralph Ramil Zamora Apr 5 '13 at 8:40
    
both the tasklist and their value will be dynamic? –  Saurabh Sinha Apr 5 '13 at 8:42
    
Saurabh, what I he goes sporting for one hour? then he need the edit the code to add the task sport to it. Not really dynamic. –  S.Visser Apr 5 '13 at 8:43
show 1 more comment

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.