考虑以下print_r()
数组
问题
有4位用户在锦标赛中对3场比赛的结果进行预测计算每支球队获得的投票数。
伪
循环数组跟踪主队,客队,平局每场比赛的投票数。
我的代码解决了上述问题
foreach($results as $result){
$homeTeam = $result['homeTeam'];
$awayTeam = $result['awayTeam'];
$draw = 'Draw';
$pickedTeam = $result['pickedTeam'];
$nrVotes = $result['nrVotes'];
$gameID = $result['gameID'];
//CHECK IF NEW MATCH OR SAME MATCH ON ITERATION
if($gameID !== $newGameID) {
if ($homeTeam === $pickedTeam) {
$homeVotes = $nrVotes;
//homeTeam Got 0 Votes
if (!isset($homeVotes)) {
$homeVotes = 0;
}
}
if ($awayTeam === $pickedTeam) {
$awayVotes = $nrVotes;
//AWAY Team Got 0 Votes
if (!isset($awayVotes)) {
$awayVotes = 0;
}
}
if ($pickedTeam === $draw) {
$drawVotes = $nrVotes;
//Draw Got Zero Votes
if (!isset($drawVotes)) {
$drawVotes = 0;
}
}
echo $homeTeam . 'Number Of Votes = ' . $homeVotes;
echo '<br />';
echo $awayTeam . 'Number Of Votes = ' . $awayVotes;
echo '<br />';
echo $draw . ' Number Of Votes = ' . $drawVotes;
}
//CHECK FOR NEW MATCH
$newGameID = $result['gameID'];
}//foreach
如果长时间盯着电脑屏幕,所以我不确定这是一个小的逻辑错误,或者我的方法是完全错误的,因为我得到一个奇怪的输出,如下面的屏幕截图所示:
请注意抽奖投票的数量似乎得到了正确的计算,但其余的却没有.... 这就是我的问题所在,如果持续数小时我一直坚持
其他信息数据库表
任何帮助都非常感激。
更新MYSQL QUERY
$sql ='SELECT picks.*, schedule.homeTeam, schedule.awayTeam, schedule.gameID, picks.team, COUNT(*) AS number_of_picks
FROM picks
JOIN schedule ON picks.gameID = schedule.gameID
WHERE picks.tournament = :tournament AND picks.weekNum = :weekNum
GROUP BY schedule.gameID, picks.team
ORDER BY schedule.gameID, picks.team';
答案 0 :(得分:3)
您的代码中存在一些问题。首先,你犯的逻辑错误在于:
if($gameID !== $newGameID)
只有在游戏ID发生变化时才会执行if
阻止,这当然只发生在每个游戏ID的第一个条目上。这就是为什么只有抽奖票是正确的。
然后有一些你从未声明的变量(例如$newGameID
)。这会产生通知而且很糟糕(即使你没有看到它,因为display_errors
可能设置为0)。
此外,您的所有isset()
都在这里:
$homeVotes = $nrVotes;
if (!isset($homeVotes)) {
由于你之前正在声明变量,因此非常无用,因此!isset()
将始终评估为false。
在这种情况下我会亲自去找一个阵列并做这样的事情:
$arr = array();
$currentGameId = -1;
foreach($results as $result) {
$gameId = $result['gameID'];
$homeTeam = $result['homeTeam'];
$awayTeam = $result['awayTeam'];
$pickedTeam = $result['pickedTeam'];
$nrVotes = $result['nrVotes'];
// initialize new subarray if game id changes
if ($gameId != $currentGameId) {
$arr[$gameId]['homeVotes'] = 0;
$arr[$gameId]['awayVotes'] = 0;
$arr[$gameId]['drawVotes'] = 0;
// remember current game id
$currentGameId = $gameId;
}
// add votes
if ($pickedTeam == $homeTeam) {
$arr[$gameId]['homeVotes'] += $nrVotes;
} elseif ($pickedTeam == $awayTeam) {
$arr[$gameId]['awayVotes'] += $nrVotes;
} else {
$arr[$gameId]['drawVotes'] += $nrVotes;
}
}
这将为您提供一个数组,游戏ID为索引,如下所示:
Array
(
[127] => Array
(
[homeVotes] => 2
[awayVotes] => 0
[drawVotes] => 2
)
[128] => Array
(
[homeVotes] => 2
[awayVotes] => 1
[drawVotes] => 1
)
)
答案 1 :(得分:2)
正如billyonecan所提到的,你的问题是
if($gameID !== $newGameID)
如果查看数组数据,您会注意到$ results [0]是匹配127的第一次出现,它给出了绘制投票的数量。你的if语句将使$ results [1]和$ results [2]被忽略。 匹配128也是如此,依此类推。
解决这个问题的方法是首先声明变量,每次更改游戏编号时,都要回显变量,然后重置它们(不要忘记使用此行中包含的信息或者你自己的变量)。我会错过一个值);
您的代码看起来像这样:
<?php
function output($homeTeam, $homeVotes, $awayTeam, $awayVotes, $draw, $drawVotes){
echo $homeTeam . ' Number Of Votes = ' . $homeVotes;
echo '<br />';
echo $awayTeam . ' Number Of Votes = ' . $awayVotes;
echo '<br />';
echo $draw . ' Number Of Votes = ' . $drawVotes;
echo '<br />';
}
$results = array(
0 => array('gameID' => '127','homeTeam' => 'Wales','awayTeam' => 'Scotland','pickedTeam' => 'Draw','nrVotes' => '3'),
1 => array('gameID' => '127','homeTeam' => 'Wales','awayTeam' => 'Scotland','pickedTeam' => 'Wales','nrVotes' => '1'),
2 => array('gameID' => '128','homeTeam' => 'France','awayTeam' => 'Ireland','pickedTeam' => 'Draw','nrVotes' => '1'),
3 => array('gameID' => '128','homeTeam' => 'France','awayTeam' => 'Ireland','pickedTeam' => 'France','nrVotes' => '2'),
4 => array('gameID' => '128','homeTeam' => 'France','awayTeam' => 'Ireland','pickedTeam' => 'Ireland','nrVotes' => '1')
);
$homeVotes = 0;
$awayVotes = 0;
$drawVotes = 0;
$newGameID = $results[0]['gameID'];
foreach($results as $result){
$homeTeam = $result['homeTeam'];
$awayTeam = $result['awayTeam'];
$draw = 'Draw';
$pickedTeam = $result['pickedTeam'];
$nrVotes = $result['nrVotes'];
$gameID = $result['gameID'];
//CHECK IF NEW MATCH OR SAME MATCH ON ITERATION
if($gameID !== $newGameID) {
// New match, output and reset
output($homeTeam, $homeVotes, $awayTeam, $awayVotes, $draw, $drawVotes);
$homeVotes = 0;
$awayVotes = 0;
$drawVotes = 0;
if ($homeTeam === $pickedTeam) {
$homeVotes = $nrVotes;
//homeTeam Got 0 Vote
if (!isset($homeVotes)) {
$homeVotes = 0;
}
}
if ($awayTeam === $pickedTeam) {
$awayVotes = $nrVotes;
//AWAY Team Got 0 Votes
if (!isset($awayVotes)) {
$awayVotes = 0;
}
}
if ($pickedTeam === $draw) {
$drawVotes = $nrVotes;
//Draw Got Zero Votes
if (!isset($drawVotes)) {
$drawVotes = 0;
}
}
}
else {
// The same match
if ($homeTeam === $pickedTeam) {
$homeVotes = $nrVotes;
//homeTeam Got 0 Vote
if (!isset($homeVotes)) {
$homeVotes = 0;
}
}
if ($awayTeam === $pickedTeam) {
$awayVotes = $nrVotes;
//AWAY Team Got 0 Votes
if (!isset($awayVotes)) {
$awayVotes = 0;
}
}
if ($pickedTeam === $draw) {
$drawVotes = $nrVotes;
//Draw Got Zero Votes
if (!isset($drawVotes)) {
$drawVotes = 0;
}
}
}
//CHECK FOR NEW MATCH
$newGameID = $result['gameID'];
}//foreach
//This line outputs the last match which would not be output otherwise.
output($homeTeam, $homeVotes, $awayTeam, $awayVotes, $draw, $drawVotes);
?>
用于更新值的if语句应放入函数中以避免复制代码。 希望它有所帮助