Guest User

Graphing PHP / Javascript

a guest
Nov 15th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Plotly.js -->
  5. <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  6. </head>
  7.  
  8.  
  9.  
  10. <body>
  11.  
  12. <p>
  13.  
  14. <?php
  15.  
  16.  
  17. if(!isset($_COOKIE["usr"])) {
  18. header("Location: WellbrosLoginTitle.html",true);
  19. } else {
  20. $user_id = $_COOKIE["usr"];
  21. }
  22.  
  23. try {
  24.  
  25. // Process form
  26. $ini_array = parse_ini_file("data.ini.php");
  27. $host = $ini_array['host'];
  28. $dbname = $ini_array['dbname'];
  29. $user = $ini_array['user'];
  30. $password = $ini_array['password'];
  31.  
  32. //print_r($ini_array);
  33. $user_id = $_COOKIE["usr"];
  34.  
  35.  
  36. $dbh = new PDO("mysql:dbname=$dbname;host=$host", $user, $password);
  37.  
  38. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  39.  
  40. $stmt = $dbh->prepare("SELECT * FROM pams_survey_attempts INNER JOIN PAMS_surveys ON PAMS_surveys.survey_id = PAMS_survey_attempts.survey_id WHERE user_id = $user_id");
  41. $stmt->execute();
  42.  
  43. $rows = $stmt->fetchAll();
  44. //print_r($rows[0]);
  45.  
  46. //echo(count($rows));
  47.  
  48. $time_array = array_fill(0, count($rows), "initialization string");
  49. $mood_rating = array_fill(0, count($rows), -999);
  50.  
  51. for($i = 0; $i < count($rows); $i++) {
  52. $row_array = $rows[$i];
  53. $time_val = $row_array["date_added"] . " " . $row_array["time_added"];
  54. $time_array[$i] = $time_val;
  55. $mood_val = $row_array["mood"];
  56. $mood_rating[$i] = $mood_val;
  57. }
  58.  
  59. //print_r($time_array);
  60. //print_r($mood_rating);
  61.  
  62. $mood_data = [ [
  63. "x" => $time_array,
  64. "y" => $mood_rating,
  65. "type" => "scatter"
  66. ] ];
  67. //foreach ($rows as $row) {
  68. //echo $row['survey_id'] . '<br />';
  69. //}
  70.  
  71. //$users = $getUsers->fetchAll();
  72. //foreach ($users as $user) {
  73. // echo $user['username'] . '<br />';
  74. //}
  75.  
  76. //foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
  77. //echo $v;
  78. //}
  79. //if ($stmt->execute(array($_POST['age_number']))) {
  80. //while ($row = $stmt->fetch()) {
  81. //echo "<pre>";
  82. //print_r($row);
  83. //}
  84. //}
  85. //$smt = foreach($pdo->query("select * from pravin_table");
  86. //print_r($smt);
  87. //foreach( $pdo->query("select * from pravin_table") as $k) {
  88. //echo "<pre>";
  89. //print_r($k);
  90. //}
  91. }
  92. catch (PDOException $e) {
  93. echo 'Connection failed: ' . $e->getMessage();
  94. }
  95. ?>
  96.  
  97. </p>
  98.  
  99.  
  100. <div id="mood_graph"></div>
  101. <script>
  102. var sample_data = [
  103. {
  104.  
  105. x: ['2013-10-04 22:23:00', '2013-12-04 22:23:00'],
  106. y: [1, 3],
  107. type: 'scatter'
  108. }
  109. ];
  110.  
  111. var layout = {
  112. title: 'Mood Rating Over Time',
  113. xaxis: {
  114. title: 'Time Point',
  115. showgrid: false,
  116. zeroline: false
  117. },
  118. yaxis: {
  119. title: 'Mood (Rating from 1-10)',
  120. showline: false
  121. }
  122. };
  123. Plotly.newPlot('mood_graph', <?php echo json_encode($mood_data) ?>, layout);
  124.  
  125. </script>
  126. <br>
  127. <center>Ideally we'd be graphing a lot more data than just the Mood over Time. We also plan to display written entries and show corellations between various data points like sleep and mood, or exercise and stress.
  128. <form action="wellBroSurvey.php"><input type="submit" value="Take Survey"></form>
  129. </center>
  130. </body>
  131.  
  132.  
  133. </html>
Add Comment
Please, Sign In to add comment