gpt4 book ai didi

php - 循环中对表中的值求和

转载 作者:行者123 更新时间:2023-11-29 00:07:36 25 4
gpt4 key购买 nike

我想仅使用一个循环对我的表中的特定列求和。有两个表:carsuteams。我想为团队中的所有用户计算 cars 的总速度。

这是我尝试过的:

function tspeed($team)
{
$sql = mysql_query("SELECT * FROM `uteams` WHERE tid='$tid'");
while ($row = mysql_fetch_array($sql))
{
$veh = get_uacar($row['uid']);
$speed = get_tspeed($row['uid']);
echo '' . $speed . '';
}
}

get_tspeed($row['uid']) 获取一个用户的速度。

$veh = get_uacar($row['uid']); 得到汽车。

我的输出是

100 150 120

但是,我想输出值的总和。我该怎么做?

最佳答案

您可以简单地使用一个变量并在每次迭代时不断添加:

$total_speed = 0;

while ($row = mysql_fetch_array($sqle)) {
$veh = get_uacar($row['uid']);
$speed = get_tspeed($row['uid']);
$total_speed += $speed; // ADD SPEED TO TOTAL
echo '' . $speed . '';
}

echo "Total Speed: " . $total_speed;

关于php - 循环中对表中的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26811884/

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