Advertisement
pcwizz

Untitled

Sep 6th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1. <?php
  2.  
  3. header("content-type: application/javascript");
  4.  
  5. require_once 'config.php';
  6.  
  7. /*
  8.  
  9. Not used, but might be handy later.
  10. -----------------------------------
  11.  
  12. function total_responses(){
  13.     $stmt = $mysqli->prepare("select COUNT(*) from responces");
  14.     $stmt->execute();
  15.     $stmt->bint_result($num_rows);
  16.     $stmt->fetch();
  17.     return $num_rows;
  18. }
  19.  
  20. function responces_for_age($age){
  21.     $stmt = $mysqli->prepare("select COUNT(responces.id) from responces join images on images.id=responces.image_id where age=?");
  22.     $stmt->bind_param("i", $age);
  23.     $stmt->execute();
  24.     $stmt->bind_result($num_rows);
  25.     $stmt->fetch();
  26.     return $num_rows;
  27. }
  28.  
  29. function average_score(){
  30.     $stmt = $mysqli->prepare("select AVG(score) from responces");
  31.     $stmt->execute();
  32.     $stmt->bind_result($avg_score);
  33.     $stmt->fetch();
  34.     return $avg_score;
  35. }
  36.  
  37. function average_score_in_time_period($time_start, $time_end){
  38.     $stmt = $mysqli->prepare("select AVG(score) from responces where datetime >= ? or datetime <= ?");
  39.     $stmt->bind_param("ii", $time_start, $time_end);
  40.     $stmt->execute();
  41.     $stmt->bind_param($avg_score);
  42.     return $avg_score;
  43. }
  44.  
  45. function average_score_for_age_in_time_period($age, $time_start, $time_end){
  46.     $stmt = $mysqli->prepare("select AVG(score) from responces join images on images.id=responces.image_id where age=? and (datetime >= ? or datetime <= ?)");
  47.     $stmt->bind_param("ii", $time_start, $time_end);
  48.     $stmt->execute();
  49.     $stmt->bind_param($avg_score);
  50.     return $avg_score;
  51. }
  52.  
  53. */
  54.  
  55. function average_score_for_age($age){
  56.     $stmt = $mysqli->prepare("select AVG(score) from responces join images on images.id=responces.image_id where age=?");
  57.     $stmt->bind_param("i", $age);
  58.     $stmt->execute();
  59.     $stmt->bind_result($avg_score);
  60.     $stmt->fetch();
  61.     return $avg_score;
  62. }
  63.  
  64. function heighest_scoring_age_in_time_period($time_start, $time_end){
  65.     $stmt = $mysqli->prepare("select age from images join responce on images.id=responce.image_id where datetime >= ? or datetime <= ? order by AVG(responce.score) desc limit 1");
  66.     $stmt->bind_param("ii", $time_start, $time_end);
  67.     $stmt->execute();
  68.     $stmt->bind_param($age);
  69.     return $age;
  70. }
  71.  
  72. function score_against_avg_age(){
  73.     $graph = array();
  74.     for ($a = MIN_AGE; $a <= MAX_AGE; $a++){
  75.         array_push($graph, array(
  76.             "age" => $a,
  77.             "score" => average_score_for_age($a)//should rewrite as stored function, as this is a lot of mysql transactions; slow!
  78.             ));
  79.     }
  80.     return $graph;
  81. }
  82.  
  83. function highest_scoring_age_againts_time(){
  84.     //find oldest responce
  85.     $stmt = $mysqli->prepare("select UNIX_TIMESTAMP(datetime) from images order by datetime asc limit 1");
  86.     $stmt->execute();
  87.     $stmt->bind_result($start_datetime);
  88.     $stmt->fetch();
  89.     //create time periods
  90.     $age_diff = MAX_AGE - MIN_AGE;
  91.     $now = time();
  92.     $total_time_elapsed = $now - $start_datetime;
  93.     $time_period = intval($total_time_elapsed / $age_diff);
  94.     $graph = array();
  95.     while ($age_diff != 0) {
  96.         $end_time = $start_datetime + $time_period;
  97.         array_push($graph, array(
  98.             "startTime" => $start_datetime,
  99.             "endTime" => $end_time,
  100.             "age" => heighest_scoring_age_in_time_period($start_datetime, $end_time)// this could also do with being an sql function.
  101.             ));
  102.         $start_datetime = $end_time;
  103.         $age_diff--;
  104.     }
  105.     return $graph;
  106. }
  107.  
  108. function output_graph($graph){
  109.     echo json_encode($graph);
  110. }
  111.  
  112.  
  113. //inspect request and serve graph data
  114. if($_POST['graph']){
  115.     switch ($_POST['graph']) {
  116.         case 'scoreAgainstAvgAge':
  117.             output_graph(scoreAgainstAvgAge());
  118.             break;
  119.        
  120.         case 'highestScoringAgeAgaintsTime':
  121.             output_graph(highest_scoring_age_againts_time());
  122.             break;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement