Reputation: 5714
I have the following function which should return all the tournaments and corresponding rounds inside an array.
When I call the function inside the body using a print_r
I get only one tournament and round returned, however there are multiple tournaments and rounds. It is like the loop exits after only one iteration
function getTournaments(){
global $db;
$sql = "SELECT tournament, weekNum
FROM schedule
GROUP BY tournament";
$stmnt = $db->prepare($sql);
$stmnt->execute();
$tournaments = $stmnt->fetchAll();
$data = array();
foreach($tournaments as $tournament){
$data = array('tournament' => $tournament['tournament'], 'round' => $tournament['weekNum']);
}//foreach
return $data;
}//function
print_r(getTournaments());
Database dump
Here you can see the corresponding mysql statement run on the db
My output / Problem
As you can see on image below I only get one tournament and round returned when doing print_r
, why am I not getting all tournaments and rounds returned inside the function array? Am I missing something here?
Upvotes: 2
Views: 52
Reputation: 595
You should have used $data as an array :) Use this
$data[] = array('tournament' => $tournament['tournament'], 'round' => $tournament['weekNum']);
instead of
$data = array('tournament' => $tournament['tournament'], 'round' => $tournament['weekNum']);
Upvotes: 0
Reputation:
you over write $data
in the loop you want to create new arrays (multidimensional):
$data[] = array('tournament' => $tournament['tournament'], 'round' => $tournament['weekNum']);
Upvotes: 3