I have done up a query builder using join with the following setup:
My Tables
Table users
user_id | username | email
1 | userA | userA@email.com
2 | userB | userB@gmail.com
Table teams
team_id | game_id | leader_user_id
5 | 1 | 1
6 | 1 | 1
7 | 2 | 1
8 | 2 | 1
Table games
game_id | game_name
1 | gameA
2 | gameB
3 | gameC
Table add_game
game_id | user_id | ign | acc_id
1 | 1 | ignA | accA
2 | 1 | ignB | accB
3 | 1 | ignC | accC
This is my current code :
Controller
public function profile()
{
$data = [];
$db = db_connect();
$model = new ProfileModel($db);
$data['profile'] = $model->getProfile();
echo view('templates/header', $data);
echo view('account/profile', $data);
echo view('templates/footer', $data);
}
}
Model:
return $this->db->table('users')
->join('add_game', 'add_game.user_id = users.user_id')
->join('teams', 'teams.leader_user_id = users.user_id')
->join('games', 'games.game_id = add_game.game_id')
->where('users.user_id', $user_id)
//->groupBy('users.user_id')
//->distinct('users.user_id')
//->select(("GROUP_CONCAT(game_id, ign, acc_id) AS userdata"))
->get()
->getResultArray();
View
<?php
$my_id = 0;
foreach($profile as $row){
if($my_id != $row['user_id']){
?>
<div><?=$row['username']?></div> <!--data from table user-->
<div><?=$row['game_name']?></div> <!--data from table add_game-->
<div><?=$row['ign']?></div>
<div><?=$row['acc_id']?></div>
<div><?=$row['team_id']?></div>
<?php
} else {
?>
<div><?=$row['game_name']?></div>
<div><?=$row['ign']?></div> <!--only data from table add_game-->
<div><?=$row['acc_id']?></div>
<div><?=$row['team_id']?></div>
<?php
}
$my_id = $row['user_id'];
}
?>
Right now I am getting many wierd duplicated data:
userA
gameA
ignA
accA
5
gameB
ignB
accB
5
gameC
ignC
accC
5
gameA
ignA
accA
6
gameB
ignB
accB
6
gameC
ignC
accC
6
gameA
ignA
accA
7
gameB
ignB
accB
7
gameC
ignC
accC
7
gameA
ignA
accA
8
gamneB
ignB
accB
8
gameC
ignC
accC
8
I want the result display to show like this :
- Display the username once
- Display all the game that have added to the add_game table once
- Display all the ign according to the game added once
- Display all the team that have added to the teams table once
I have totally no clue how am I going to display my results without getting duplicates. Also, what or how do I need to do if I have a few more tables after that?