繁体   English   中英

如何从 PHP 中的更多 mySQL 行中获取平均值?

[英]How to make an average value from more mySQL rows in PHP?

我不懂任何 PHP,但我需要一个 API。 我的朋友做了一个,但我需要平均时间戳(当然,如果查询发现不止一行)。 从我的 mysql 数据库中,我以字符串形式获得时间戳 (00:34:51)。

我想我知道该怎么做,但是我在 PHP 方面太无能了,以至于我无法自己修复它。

这是网络 API

<?php

// import connect
require_once '../db_connect.php';

// api input
$name = $_POST['name'];
$country = $_POST['country'];
$race = $_POST['race_type'];

$return = [];

// execute query
$stmt = $conn->prepare("SELECT Swim,Bike,Run,Overall FROM test WHERE Name = ? AND Country = ? AND Race = ?");
$stmt->bind_param("sss", $name, $country, $race);
$stmt->execute();
$result = $stmt->get_result();

// search in data
while ($row = $result->fetch_assoc()) {
    if ($row['Swim'] < $row['Bike'] && $row['Swim'] < $row['Run']) {
        $best = $row['Swim'];
    } else if ($row['Bike'] < $row['Swim'] && $row['Bike'] < $row['Run']) {
        $best = $row['Bike'];
    } else {
        $best = $row['Run'];
    }

    array_push($return, [
        'swim' => $row['Swim'],
        'bike' => $row['Bike'],
        'run' => $row['Run'],
        'overall' => $row['Overall'],
        'best' => $best
    ]);
}

// return data
echo json_encode($return);

$stmt->close();
$conn->close();

die();

这就是 mySQL 数据库中一行的样子

ID  Race    RaceDate    Name    Country Div Rank    Gender Rank Overall Rank    Swim    Bike    Run Finish
1   70.3 Pula   20170917    Stefan Haubner  DEU 1   1   1   0:00:34 2:23:03 1:20:33 3:46:33

据我所知,时间戳以小时:分钟:秒的格式存储。 要找到四个时间戳(游泳、自行车、跑步、总体)中的最小值,您需要首先将每个时间戳转换为秒。 以下代码可用于获取四个时间戳的最佳时间:

function GetTimeInSec($time) {
    list($hour, $min, $sec) = explode(":", $time);
    $hour                   = (int) ltrim($hour, "0");
    $min                    = (int) ltrim($min, "0");
    $sec                    = (int) ltrim($sec, "0");
    $new_time               = (($hour * 3600) + ($min * 60) + ($sec));

    return $new_time;
}

function GetBestTime($t1, $t2, $t3, $t4) {
    $time_arr   = array(
                      GetTimeInSec($t1) => $t1, 
                      GetTimeInSec($t2) => $t2,
                      GetTimeInSec($t3) => $t3,
                      GetTimeInSec($t4) => $t4
                  );
    ksort($time_arr);
    $time_cols  = array_keys($time_arr);
    $best_time  = $time_cols[0];                

    return $best_time;
}

$time_data = GetBestTime($row['Swim'], $row['Bike'], $row['Run'], $row['Overall']);

$best_time = $time_data["best_time"];
$avg_time  = $time_data["avg_time"];

自行车、跑步和游泳的平均时间可以通过首先将每个值从 VARCHAR 转换为 INT 格式的秒数来计算。 之后,array_sum 函数可用于查找游泳、自行车和跑步的总和。 然后将总和除以总行数以获得平均值。 可以使用以下代码:

function GetAverageTimes($data) {
    $data_new        = array("swim" => array(), "bike" => array(), "run" => array());
    for ($count = 0; $count < count($data); $count++) {
        $row       = $data[$count];
        array_push($data_new['swim'], GetTimeInSec($row['swim']));
        array_push($data_new['bike'], GetTimeInSec($row['bike']));
        array_push($data_new['run'], GetTimeInSec($row['run']));                        
    }

    $averages         = array("swim" => 0, "bike" => 0, "run" => 0);
    $averages['swim'] = ceil(array_sum($data_new['swim']) / count($data));
    $averages['bike'] = ceil(array_sum($data_new['bike']) / count($data));        
    $averages['run']  = ceil(array_sum($data_new['run']) / count($data));

    return $averages;
}

$average_times = GetAverageTimes($return);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM