php - How to create array using javascript with a database mysql? -
anyone can me problem thank in advance google chart need change task , hours per day dynamically, need database can connect database , put them on array how can 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>
first need design database structure. example:
** activities ** | id | task | hours | | 1 | work | 11 | | 2 | eat | 2 | | 3 | cummute | 2 | | 4 | watch tv | 2 | | 5 | sleep | 7 |
now need make database connection. ( easiest way, there better ways this, example )
<?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('not connected : ' . mysql_error()); } // make foo current db $db_selected = mysql_select_db('your_db_name', $link); if (!$db_selected) { die ('can\'t use database : ' . mysql_error()); } ?>
you need build code dynamic:
var data = google.visualization.arraytodatatable([ ['task', 'hours per day'], ['work', 11], ['eat', 2], ['commute', 2], ['watch tv', 2], ['sleep', 7] ]);
in example, title static
var data = google.visualization.arraytodatatable([ ['task', 'hours per day'], <?php $query = mysql_query("select * 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 } ?> ]);
Comments
Post a Comment