从数组中拉出特定值进行计算

时间:2017-11-10 16:48:07

标签: php sql

我有2张桌子。表1是举行每周游戏的时间表。表2是一个单独的表格,您可以从一周的预定游戏中选择一个团队。 我试图让我选择一支球队的比赛得分有所不同。因此,对于特定的一周,有13-16场比赛。我从其中一个游戏中选择了一个团队。如果我选择的球队获胜,结果就是得分差异。因此,如果我的球队获胜并且得分为27-10,我会显示17分。我已经尝试过每一种我能想到的方法来获得这个,但我似乎想到的最好的是它只计算一周的最后一场比赛,而不是我的团队参与的特定游戏.gameID是关键两个表之间。是否有可能做到这一点?我想通过从数组中获取基于gameID的值,它会将它与与表2中的选择相关联的gameID匹配。我能够逐周显示正确的团队,但不能获得该特定游戏的点差异。有什么想法吗?

<?php
for($wk=1;$wk<=17;$wk++){
    $allScoresIn = true;
    $currentDT = date('Y-m-d H:i:s');
    //get array of games
    $games = array();
    $sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "')  as expired ";
    $sql .= "from " . DB_PREFIX . "schedule s ";
    $sql .= "where s.weekNum = " . $wk . " ";
    $sql .= "order by s.gameTimeEastern, s.gameID";

    $query = $mysqli->query($sql);
    if ($query->num_rows > 0) {
        $e = 0;
        $homePtDiff = 0;
        $visitorPtDiff = 0;
        while ($row = $query->fetch_assoc()) {
            $games[$row['gameID']]['gameID'] = $row['gameID'];
            $games[$row['gameID']]['homeID'] = $row['homeID'];
            $games[$row['gameID']]['visitorID'] = $row['visitorID'];
            $games[$row['gameID']]['homeScore'] = $row['homeScore'];
            $games[$row['gameID']]['visitorScore'] = $row['visitorScore'];
            $games[$row['gameID']]['homeDiff'][$e] = $row['homeScore'] - $row['visitorScore'];
            $games[$row['gameID']]['visitorDiff'][$e] = $row['visitorScore'] - $row['homeScore'];
            $homePtDiff = $row['homeScore'] - $row['visitorScore'];
            $visitorPtDiff = $row['visitorScore'] - $row['homeScore'];
            if ((int)$row['homeScore'] != NULL && (int)$row['visitorScore'] != NULL) {
                $scoreEntered = TRUE;
            }else{
                $scoreEntered = FALSE;
            }
            if ((int)$games[$row['gameID']]['homeScore'] > (int)$games[$row['gameID']]['visitorScore']) {
                $games[$row['gameID']]['winnerID'] = $row['homeID'];
            }else if ((int)$games[$row['gameID']]['homeScore'] < (int)$games[$row['gameID']]['visitorScore']){
                $games[$row['gameID']]['winnerID'] = $row['visitorID'];
            }
            else{
                $games[$row['gameID']]['winnerID'] = NULL;
            }
            $e++;
        }
    }
    $sqlinner = "select * from " . DB_PREFIX . "pickmargin where weekNum = " . $wk . " and userID = " . $x . ";";
    $queryinner = $mysqli->query($sqlinner);
    if ($queryinner->num_rows > 0) {
        $resultinner = $queryinner->fetch_assoc();
        $currentPick = $resultinner['pickmargin'];
        $currentGameID = $resultinner['gameID'];
        $hidePicks = $resultinner['showPicks'];
        $marginPts = 0;
        $y_value = $x_value-1;
    } else {
        $currentPick = 'TBD';
    }
    if ($currentPick == $games[$row['gameID']]['homeID']){
        $marginPts = (int)$games[$row['gameID']]['homeScore'] - (int)$games[$row['gameID']]['visitorScore'];
    }
    else{
        $marginPts = (int)$games[$row['gameID']]['visitorScore'] - (int)$games[$row['gameID']]['homeScore'];
    }
    // ...
}

0 个答案:

没有答案