I have a question about a sql query that will return an array. ( or if it's not possible, maybe there is a way to solve this with an array function in php? )
Here's an example of my database and tables
table member
id name firstname active
member1 Doe John 1
member2 Doe Jane 1
member3 Doe Duncan 0
member4 Doe Marc 1
table member_meeting
member_id meeting_id options
1 1 1
1 2 1,2
2 1 2,3,6
2 3 2
4 3 4
4 5 2,3,4
table meeting
id start_date active
meeting1 2018-05-21 1
meeting2 2018-05-21 1
meeting3 2019-05-21 0
meeting4 2020-05-20 1
meeting5 2017-05-20 1
meeting6 2020-05-50 1
....
What I would like have returned
m_1 m_2 m_4 m_6 ...
member1 options options NULL NULL
member2 options NULL NULL NULL
member4 NULL NULL options NULL
So when e member isn't active, it shouldn't be in the end results. When a meeting is already over/ or not active, it also can't be in the end results. I don't know the amount of members, or meetings. they can change per new meeting request. Can this be returned by sql? or should this be done in php arrays? Another problem, I tried with php, but I ended up with a foreach in a foreach in a foreach. Those are tables with a lot of rows, and I ended up with a very slow loadtime.
Now at the moment I solved it like below. But is there a better way to solve it? I don't like the multiple for each loops. ( And I'm still searching for the same result but with SQL query )
meeting_member is a select query where I add member_id and meeting_id in 1 column so I can use it as keys
$meeting_matrix = array(
'name' => array(0 => 'neme'),
);
foreach ($meetings as $meeting) {
$meeting_matrix['name'][$meeting->id] = $meeting->title;
}
foreach ($members as $member) {
$options = array();
$options[] = $member->name . ' ' . $member->firstname;
foreach ($meetings as $meeting) {
//start_date
$options[] = (!empty($meeting_member[$meeting->id . '_' . $member->id]))
? $meeting_member[$meeting->id . '_' . $member->id]->option : "";
}
$meeting_matrix[$member->name . ' ' . $member->firstname] = $options;
}