循环数组时未初始化的字符串偏移量

时间:2016-04-13 14:34:22

标签: php arrays

我试图编写一个Pick'em应用程序,用户选择哪个团队将赢得锦标赛中每个ROUND游戏。 I.E锦标赛有多轮比赛,我每轮计算获胜者并记录胜利。

我目前正在努力让胜利者在一轮结束时

我解决上述问题的逻辑如下:

  • 获取活动信息
  • 获取会员选择
  • 获得团队
  • 获得选择正确团队的成员(我的问题在这里!)
  • 统计哪位会员选择了最正确的轮次

我有一个DB结构部分看起来像这样

enter image description here

我在下面发布我的代码,我希望有人可以指出我哪里出错了。

我可以确认以下

  • 第1步测试 - 工作
  • 第2步测试 - 工作
  • 第3步测试 - 工作
  • 步骤 - 4我的问题错误(void playercheck() { cout << "It is Player: " << PlayerPiece << " Turn, please put select a piece on the board" << endl; cin >> playerchoice; //while the input it less than 1, more than 9, or NOT an integer while (playerchoice < 1 || playerchoice > 9 || cin.fail()) { cout << "You did not enter an appropriate response." << endl; cout << "Please, enter in a digit between 1 and 9." << endl; //clear the input flag on the stream so we can recheck cin.fail() cin.clear(); //skip up to 256 characters in the pursuit of skipping to the next newline cin.ignore(256, '\n'); cin >> playerchoice; } playerinput(playerchoice); }
  • 步骤 - 5首先修复第4步

// ### STEP 1 ###

Uninitialized string offset

坚持了几个小时任何帮助或建议非常感谢

1 个答案:

答案 0 :(得分:1)

假设您的第一个查询只返回一个事件,您可以找到谁选择正确的团队:

//GET EVNT INFO
$sql   = "SELECT * FROM events WHERE tournament = 'IPL' AND round = '5'";
$query = $db->query($sql);

// Store the event info inside an array.
$event = $query->fetch(PDO::FETCH_ASSOC);

//### STEP 2 ###
//GET MEMBER SELECTIONS
$sql   = "SELECT * FROM multiple_picks WHERE tournament = 'IPL'  AND round ='5' ORDER BY round_game_nr";
$query = $db->query($sql);

// Store members inside a collection of member.
$picks = $query->fetchAll(PDO::FETCH_ASSOC);

//### STEP 3 ###
//GET Winning Teams
$winner = null;
if ($event['team1Score'] > $event['team2Score']) {
    $winner = $event['team1'];
} elseif ($event['team1Score'] < $event['team2Score']) {
    $winner = $event['team2'];
}

// ### STEP 4 GET MEMBERS WHO SELECTED CORRECT TEAM ###
//GET WINNING MEMBERS
$correct = [];
foreach ($picks as $pick) {
    if ($pick['pick'] === $winner) {
        $correct[] = $pick['member_nr'];
        echo 'Member ' . $pick['member_nr'] . ' picks correctly !';
    }
}