Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- What is optimum age?
- ~~~~~~~~~~~~~~~~~~~~
- Description
- ====================================================================
- An on-line serve looking at at what age females are most attractive.
- ====================================================================
- Copyright (C) 2014 Morgan Hill <morgan@pcwizzltd.com>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- header("content-type: application/javascript");
- require_once 'config.php';
- /*
- Not used, but might be handy later.
- -----------------------------------
- function total_responses(){
- $stmt = $mysqli->prepare("select COUNT(*) from responses");
- $stmt->execute();
- $stmt->bint_result($num_rows);
- $stmt->fetch();
- return $num_rows;
- }
- function responses_for_age($age){
- $stmt = $mysqli->prepare("select COUNT(responses.id) from responses join images on images.id=responses.image_id where age=?");
- $stmt->bind_param("i", $age);
- $stmt->execute();
- $stmt->bind_result($num_rows);
- $stmt->fetch();
- return $num_rows;
- }
- function average_score(){
- $stmt = $mysqli->prepare("select AVG(score) from responses");
- $stmt->execute();
- $stmt->bind_result($avg_score);
- $stmt->fetch();
- return $avg_score;
- }
- function average_score_in_time_period($time_start, $time_end){
- $stmt = $mysqli->prepare("select AVG(score) from responses where datetime >= ? or datetime <= ?");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($avg_score);
- return $avg_score;
- }
- function average_score_for_age_in_time_period($age, $time_start, $time_end){
- $stmt = $mysqli->prepare("select AVG(score) from responses join images on images.id=responses.image_id where age=? and (datetime >= ? or datetime <= ?)");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($avg_score);
- return $avg_score;
- }
- */
- function average_score_for_age($age){
- $stmt = $mysqli->prepare("select AVG(score) from responses join images on images.id=responses.image_id where age=?");
- $stmt->bind_param("i", $age);
- $stmt->execute();
- $stmt->bind_result($avg_score);
- $stmt->fetch();
- return $avg_score;
- }
- function heighest_scoring_age_in_time_period($time_start, $time_end){
- $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");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($age);
- return $age;
- }
- function score_against_avg_age(){
- $graph = array();
- for ($a = MIN_AGE; $a <= MAX_AGE; $a++){
- array_push($graph, array(
- "age" => $a,
- "score" => average_score_for_age($a)//should rewrite as stored function, as this is a lot of mysql transactions; slow!
- ));
- }
- return $graph;
- }
- function highest_scoring_age_againts_time(){
- //find oldest response
- $stmt = $mysqli->prepare("select UNIX_TIMESTAMP(datetime) from images order by datetime asc limit 1");
- $stmt->execute();
- $stmt->bind_result($start_datetime);
- $stmt->fetch();
- //create time periods
- $age_diff = MAX_AGE - MIN_AGE;
- $now = time();
- $total_time_elapsed = $now - $start_datetime;
- $time_period = intval($total_time_elapsed / $age_diff);
- $graph = array();
- while ($age_diff != 0) {
- $end_time = $start_datetime + $time_period;
- $time = new DateTime($start_datetime + ($end_time - $start_datetime));
- $time->setTimestamp();
- array_push($graph, array(
- "time" => $time,
- "age" => heighest_scoring_age_in_time_period($start_datetime, $end_time)// this could also do with being an sql function.
- ));
- $start_datetime = $end_time;
- $age_diff--;
- }
- return $graph;
- }
- function output_graph($graph){
- echo json_encode(array("point" => $graph);
- }
- //inspect request and serve graph data
- if($_POST['graph']){
- switch ($_POST['graph']) {
- case 'scoreAgainstAvgAge':
- output_graph(scoreAgainstAvgAge());
- break;
- case 'highestScoringAgeAgaintsTime':
- output_graph(highest_scoring_age_againts_time());
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement