我对php很新。我遇到嵌套的foreach循环问题。好像我无法读取$ row中的数据,例如。 $行[' Tournament_NB_ID1']
我尝试在foreach循环之前使用while循环遍历$ arr_tournaments并且数组中有数据。此外,我已经尝试更改服务器的PHP版本,似乎没有任何帮助。代码......
include 'variable.php';
include 'db_connect.php';
//Connect to database
db_connect();
$arr_tournaments = mysqli_query($GLOBALS['db_con'], "SELECT * FROM tournaments");
echo 'Current PHP version: ' . phpversion() . '<br>';
print_r ($arr_tournaments);
//$xml = simplexml_load_file("football.xml");
$xml = simplexml_load_file($xml_nordicbet_football); //Live file
foreach ($xml->children() as $game) {
foreach ($arr_tournaments as $row) {
if ($game->BreadCrumbs == $row['Tournament_NB_ID1']) {
$id = $game['id'];
$type = '1X2'; //Type of bet
$season = $game->Season;
$breadcrumbs = $game->Game->BreadCrumbs;
$gamestarttime = $game->GameStartTime;
echo 'Loop1';
foreach ($game->OutcomeSet as $outcomeset) {
if ($outcomeset['type'] == $row['Tournament_NB_ID2']) {
echo 'loop2';
$outcomeset_type = $outcomeset['type'];
echo $outcomeset_type;
foreach ($outcomeset->Outcome as $outcome) {
switch ($outcome['name']) {
case '1':
$outcome_odds1 = $outcome['odds'];
$outcome_p1 = utf8_decode($outcome->Participant); //utf8_decode converts to iso ISO-8859-1 (has æøå)
case 'X':
$outcome_oddsX = $outcome['odds'];
case '2':
$outcome_odds2 = $outcome['odds'];
$outcome_p2 = utf8_decode($outcome->Participant);
}
}
}
}
$sql = "INSERT INTO ogames (Game_NB_ID, Participant1, Participant2, TournamentID, Odds_1, Odds_X, Odds_2, StartDateTime, GameType)
VALUES
('$id', '$outcome_p1', '$outcome_p2', '$row[TournamentID]', '$outcome_odds1', '$outcome_oddsX', '$outcome_odds2', '$gamestarttime', '$type')
ON DUPLICATE KEY UPDATE
Odds_1 = $outcome_odds1, Odds_X = $outcome_oddsX, Odds_2 = $outcome_odds2";
// Does not update start time!!
if (!mysqli_query($db_con, $sql)) {
die('Error: ' . mysqli_error($db_con));
}
echo "1 record added" . "<br>";
}
}
}
mysqli_close($db_con);