Im learning PHP and trying to move on to a bit more advanced "stuff" my lecture advised us one of the best ways to learn is working / looking through / playing with opensource code.
Im currently inspecting an opensource project's code, and trying to slightly tweak the code.
The function is as follows:
function calculateStats() {
global $mysqli, $weekStats, $playerTotals, $possibleScoreTotal;
//get latest week with all entered scores
$lastCompletedWeek = getLastCompletedWeek();
//loop through weeks
for ($week = 1; $week <= $lastCompletedWeek; $week++) {
//get array of games
$games = array();
$sql = "select * from schedule where weekNum = " . $week . " order by gameTime, gameID";
$query = $mysqli->query($sql);
while ($row = $query->fetch_assoc()) {
$games[$row['gameID']]['gameID'] = $row['gameID'];
$games[$row['gameID']]['homeID'] = $row['homeID'];
$games[$row['gameID']]['awayID'] = $row['visitorID'];
if ((int)$row['homeScore'] > (int)$row['visitorScore']) {
$games[$row['gameID']]['winnerID'] = $row['homeID'];
}
if ((int)$row['visitorScore'] > (int)$row['homeScore']) {
$games[$row['gameID']]['winnerID'] = $row['visitorID'];
}
}
$playerPicks = array();
$playerWeeklyTotals = array();
$sql = "select p.userID, p.gameID, p.pickID, p.points, u.firstname, u.lastname, u.userName ";
$sql .= "from picks p ";
$sql .= "inner join users u on p.userID = u.userID ";
$sql .= "inner join schedule s on p.gameID = s.gameID ";
$sql .= "where s.weekNum = " . $week . " and u.userName <> 'admin' ";
$sql .= "order by u.lastname, u.firstname, s.gameTime";
$query = $mysqli->query($sql);
while ($row = $query->fetch_assoc()) {
$playerPicks[$row['userID'] . $row['gameID']] = $row['pickID'];
$playerWeeklyTotals[$row['userID']][week] = $week;
$playerTotals[$row['userID']][wins] += 0;
$playerTotals[$row['userID']][name] = $row['firstname'] . ' ' . $row['lastname'];
$playerTotals[$row['userID']][userName] = $row['userName'];
if (!empty($games[$row['gameID']]['winnerID']) && $row['pickID'] == $games[$row['gameID']]['winnerID']) {
//player has picked the winning team
$playerWeeklyTotals[$row['userID']][score] += 1;
$playerTotals[$row['userID']][score] += 1;
} else {
$playerWeeklyTotals[$row['userID']][score] += 0;
$playerTotals[$row['userID']][score] += 0;
}
}
}
}
echo calculateStats();
When I call the function, I get the following error message:
Use of undefined constant week - assumed 'week'
Use of undefined constant wins - assumed 'wins'
It gives the same error message for each index in the loop which does not contain '' (quotes)
When I do add '' (quotes) between the constants in the loop I get an undefined index error
Question
- Why are they using constants inside the loop on array variables?
- Why am I getting the error
Use of undefined constant week - assumed 'week'
when calling the function, and what steps can I take to correct it? - Why are the following variables declared as global
global $weekStats, $playerTotals, $possibleScoreTotal;
?
NOTE: to prevent the code from getting to long in the question I would like add line 4 inside the function $lastCompletedWeek = getLastCompletedWeek();
works correctly i.e. getLastCompletedWeek()
returns correct result so correct result is passed to $lastCompletedWeek
inside function.
I hope my question makes sense, if you need any more information please let me know, thank you very much!
The link of the project can be found here