gpt4 book ai didi

php - 将加权平均值添加到返回的mysql查询并将所有数据相加

转载 作者:行者123 更新时间:2023-11-29 23:10:09 25 4
gpt4 key购买 nike

我对 php 比较陌生...我有两个查询通过另一个字段名称返回列的平均值。我需要为这些平均值添加权重,然后将结果相加以获得总分...我无法让它工作...以下是查询:

$engresults = $con->query("SELECT AVG(activity) as avgscore FROM combinedna WHERE coachname = '$coachname'");    
$timeresults = $con->query("SELECT AVG(timely) as avgtime FROM combinedna WHERE coachname = '$coachname'");

平均值返回正确,我可以向该函数添加权重,但无法将两者相加

while($row = $engresults->fetch_assoc()) {
echo "<tr>";
echo "<td>Did you identify an effective integrated activity?</td>";
echo "<td><center>" .$row['avgscore'] *.05 . "</center></td>";
echo "</tr>";
}

while($row = $timeresults->fetch_assoc()) {
echo "<tr>";
echo "<td>Did you spend the appropriate amount of time?</td>";
echo "<td><center> " .$row['avgtime'] *.10 . "</center></td>";
echo "</tr>";
}

我需要将 .05 添加到第一个,将 .10 添加到第二个,然后将结果加在一起......我只是不知道要使用的 PHP。预先感谢您的耐心和帮助

最佳答案

将值放入变量中,然后添加它们。

$row = $engresults->fetch_assoc(); // No loop needed
$avgscore_weighted = $row['avgscore'] * .05;
$row = $timeresults->fetch_assoc();
$avgtime_weighted = $row['avgtime'] * .10;
$weighted_sum = $avg_score_weighted + $avgtime_weighted;

echo "<tr>";
echo "<td>Did you identify an effective integrated activity?</td>";
echo "<td><center>" .$avgscore_weighted . "</center></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Did you spend the appropriate amount of time?</td>";
echo "<td><center> " .$avgtime_weighted . "</center></td>";
echo "</tr>";

echo "<tr>";
echo "<td>The total was: </td>";
echo "<td><center> " .$weighted_sum . "</center></td>";
echo "</tr>";

关于php - 将加权平均值添加到返回的mysql查询并将所有数据相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com