0

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>
2
  • where is the php? you need php to connect to mysql database Commented Apr 5, 2013 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] ]); Commented Apr 5, 2013 at 8:37

2 Answers 2

3

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
         }
       ?>
    ]);
Sign up to request clarification or add additional context in comments.

7 Comments

dont use mysql functions always use mysqli functions.
@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...
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.
if($i == $count) { $last = ','; ) this line will produce an error. why do we need to have that last paranthesis
Thank you Visser I will try your code now. I will send a feedback later and thank you to all who has a suggestion.
|
-1

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.

5 Comments

@S.Visser: why not dynamic, could you please explain
I need the "Task" and "Hours per day" to be dynamic and it comes to the database anyone can update the code.
both the tasklist and their value will be dynamic?
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.
yup for example I added a new "task" equal to Listening and "Hours" equal to 3 and saved to the database how can I output that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.