• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter 4 display records using join with multiple tables

#3
(09-21-2020, 11:39 PM)ojmichael Wrote: You have a few options here.. recursive SQL (slow), some hideous GROUP_CONCAT or a better proposed solution below.

PHP Code:
$teams $this->db->table('team_list t1')
    ->join('teammember_list t2''t2.team_id = t1.team_id')
    ->join('game_list t3''t3.game_id = t2.game_id')
    ->join('add_game t4''t4.game_id = t3.game_id')
    ->where('t2.user_id'$user_id)
    ->get()
    ->getResultArray();

$teamIds array_column($teams'team_id');

$teamMembers $this->db->table('teammember_list t2')
    ->whereIn('t2.team_id'$teamIds)
    ->get()
    ->getResultArray();

foreach (
$teams AS &$team) {
    $team['members'] = array_filter(
        $teamMembers,
        fn($teamMember) => $teamMember['team_id'] === $team['team_id']
    );


Then you can loop through your teams for the table rows, and in the member column you can loop through the $team['members']

Hi, thank you so much for your help! But how do I do that? Do I return teams? or do I return teamIds? Sorry I am new to this and I still don't know how all these works.
Reply


Messages In This Thread
RE: Codeigniter 4 display records using join with multiple tables - by Dr3am3rz - 09-22-2020, 04:09 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.