【发布时间】:2017-10-19 11:05:46
【问题描述】:
我有一个在线游戏的评分系统,它基于人们根据玩家列表创建团队。
一旦输入玩家得分字段,下面的脚本应该根据预定义的分数乘数计算分数并更新 3 个表格中的数据
- 球员表(我更新球员 score1 和 score2 以及他的分数)
- 分数表(我在其中为每个“相同字段”的玩家添加新分数)
- 更新所有球队 score1、 score2 和该球员 ID 所在的分数
这里简要介绍一下我的数据库结构
球员表
- 身份证
- 得分1
- 得分2
- 总计
成绩表
- 身份证
- 玩家ID
- 得分1
- 得分2
- 总计
- 时间
团队表
- 身份证
- 用户名
- p1
- p2
- b3
- b4
- score1(这是所有 4 名玩家得分的总和)
- score2(这是所有 4 名玩家得分的总和)
- 总计(这是所有 4 名玩家的所有计算得分的总和)
- 时间
问题
令人惊讶的是,第 1 步和第 2 步完美运行,分数已添加,球员已正确添加,但当我进入第 3 步时,一些球队得到了正确更新,其中一些球队没有更新正确的分数
我的代码
//predefined points multiplier
define("points1", 7);
define("points2", 8);
if (isset($_POST['submit'])) {
$id = $_POST['id'];
$score1= $_POST['score1'];
$score2 = $_POST['score2'];
foreach($id as $key=>$player){
//calculate the total points based on their predefined multipliers
$totalpoints = 0;
$totalpoints += $score1[$key] * points1;
$totalpoints += $score2[$key] * points2;
//update player table
#send a request to api to update this player data
//add scores
#send a request to api to add the new scores to the score table
//update teams (updates the teams for users)
$raw = [
'id' => $id,
'score1' => $score1[$key],
'score2' => $score2[$key],
'total' => $totalpoints
];
$data = http_build_query ($raw);
$result = file_get_contents(
BASE_URL . 'update',
false,
stream_context_create(array(
PROTOCOL => array(
'method' => 'PUT',
'header' => array(
'Content-Length: ' . strlen($data),
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
),
'content' => $data
)
))
);
$response = json_decode($result, false);
}//end foreach
echo $response->notice->message;
}
【问题讨论】:
-
首先突出的是
$totalpoints += $score1[$key] * points1;&$totalpoints += $score2[$key] * points2;应该在foreach循环内。 -
@PeterM 但这是我用来更新球员和得分表的同一个变量,除了更新球队之外,它在所有这些上都可以正常工作,我会把它放进去,但为什么它会更新正确有时团队总分 score1 和 score2 有时却没有
-
那你在哪里设置
$key部分呢?除非你有它在上面的某个地方或另一个正在处理的文件中。 -
它在
foreach($id as $key=>$player)中设置