Advertisement
pcwizz

Untitled

Sep 10th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     What is optimum age?
  5.     ~~~~~~~~~~~~~~~~~~~~
  6.  
  7.     Description
  8.     ====================================================================
  9.  
  10.     An on-line serve looking at at what age females are most attractive.
  11.  
  12.     ====================================================================
  13.  
  14.     Copyright (C) 2014  Morgan Hill <morgan@pcwizzltd.com>
  15.  
  16.     This program is free software: you can redistribute it and/or modify
  17.     it under the terms of the GNU General Public License as published by
  18.     the Free Software Foundation, either version 3 of the License, or
  19.     (at your option) any later version.
  20.  
  21.     This program is distributed in the hope that it will be useful,
  22.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.     GNU General Public License for more details.
  25.  
  26.     You should have received a copy of the GNU General Public License
  27.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  28.  
  29. */
  30.  
  31. header("content-type: application/javascript");
  32.  
  33. require_once 'config.php';
  34.  
  35. /*
  36.  
  37. Not used, but might be handy later.
  38. -----------------------------------
  39.  
  40. function total_responses(){
  41.     $stmt = $mysqli->prepare("select COUNT(*) from responses");
  42.     $stmt->execute();
  43.     $stmt->bint_result($num_rows);
  44.     $stmt->fetch();
  45.     return $num_rows;
  46. }
  47.  
  48. function responses_for_age($age){
  49.     $stmt = $mysqli->prepare("select COUNT(responses.id) from responses join images on images.id=responses.image_id where age=?");
  50.     $stmt->bind_param("i", $age);
  51.     $stmt->execute();
  52.     $stmt->bind_result($num_rows);
  53.     $stmt->fetch();
  54.     return $num_rows;
  55. }
  56.  
  57. function average_score(){
  58.     $stmt = $mysqli->prepare("select AVG(score) from responses");
  59.     $stmt->execute();
  60.     $stmt->bind_result($avg_score);
  61.     $stmt->fetch();
  62.     return $avg_score;
  63. }
  64.  
  65. function average_score_in_time_period($time_start, $time_end){
  66.     $stmt = $mysqli->prepare("select AVG(score) from responses where datetime >= ? or datetime <= ?");
  67.     $stmt->bind_param("ii", $time_start, $time_end);
  68.     $stmt->execute();
  69.     $stmt->bind_param($avg_score);
  70.     return $avg_score;
  71. }
  72.  
  73. function average_score_for_age_in_time_period($age, $time_start, $time_end){
  74.     $stmt = $mysqli->prepare("select AVG(score) from responses join images on images.id=responses.image_id where age=? and (datetime >= ? or datetime <= ?)");
  75.     $stmt->bind_param("ii", $time_start, $time_end);
  76.     $stmt->execute();
  77.     $stmt->bind_param($avg_score);
  78.     return $avg_score;
  79. }
  80.  
  81. */
  82.  
  83. function average_score_for_age($age){
  84.     $stmt = $mysqli->prepare("select AVG(score) from responses join images on images.id=responses.image_id where age=?");
  85.     $stmt->bind_param("i", $age);
  86.     $stmt->execute();
  87.     $stmt->bind_result($avg_score);
  88.     $stmt->fetch();
  89.     return $avg_score;
  90. }
  91.  
  92. function heighest_scoring_age_in_time_period($time_start, $time_end){
  93.     $stmt = $mysqli->prepare("select age from images join responses on images.id=responses.image_id where datetime >= ? or datetime <= ? order by AVG(response.score) desc limit 1");
  94.     $stmt->bind_param("ii", $time_start, $time_end);
  95.     $stmt->execute();
  96.     $stmt->bind_param($age);
  97.     return $age;
  98. }
  99.  
  100. function score_against_avg_age(){
  101.     $graph = array();
  102.     for ($a = MIN_AGE; $a <= MAX_AGE; $a++){
  103.         array_push($graph, array(
  104.             "age" => $a,
  105.             "score" => average_score_for_age($a)//should rewrite as stored function, as this is a lot of mysql transactions; slow!
  106.             ));
  107.     }
  108.     return $graph;
  109. }
  110.  
  111. function highest_scoring_age_againts_time(){
  112.     //find oldest response
  113.     $stmt = $mysqli->prepare("select UNIX_TIMESTAMP(datetime) from images order by datetime asc limit 1");
  114.     $stmt->execute();
  115.     $stmt->bind_result($start_datetime);
  116.     $stmt->fetch();
  117.     //create time periods
  118.     $age_diff = MAX_AGE - MIN_AGE;
  119.     $now = time();
  120.     $total_time_elapsed = $now - $start_datetime;
  121.     $time_period = intval($total_time_elapsed / $age_diff);
  122.     $graph = array();
  123.     while ($age_diff != 0) {
  124.         $end_time = $start_datetime + $time_period;
  125.         $time = new DateTime($start_datetime + ($end_time - $start_datetime));
  126.         $time->setTimestamp();
  127.         array_push($graph, array(
  128.             "time" => $time,
  129.             "age" => heighest_scoring_age_in_time_period($start_datetime, $end_time)// this could also do with being an sql function.
  130.             ));
  131.         $start_datetime = $end_time;
  132.         $age_diff--;
  133.     }
  134.     return $graph;
  135. }
  136.  
  137. function output_graph($graph){
  138.     echo json_encode(array("point" => $graph);
  139. }
  140.  
  141.  
  142. //inspect request and serve graph data
  143. if($_POST['graph']){
  144.     switch ($_POST['graph']) {
  145.         case 'scoreAgainstAvgAge':
  146.             output_graph(scoreAgainstAvgAge());
  147.             break;
  148.        
  149.         case 'highestScoringAgeAgaintsTime':
  150.             output_graph(highest_scoring_age_againts_time());
  151.             break;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement