2

I have done up a query builder using join. I would like to show table 2, 3, 4, 5, 6 and so on based on the user id on table 1. I tried to query the result, it is showing like this :

enter image description here

My Tables

Table users

user_id | username | email          
1       | userA    | userA@email.com
2       | userB    | userB@gmail.com
                                                         
Table add_game
game_id | user_id | ign    | acc_id
1       | 1       | ignA   | accA
2       | 1       | ignB   | accB
1       | 2       | ignB   | accB
3       | 2       | ignD   | accD 

I will be using foreach loop and I believe it will display out multiple times based on the records in the database. What should I do if I only want to display the information highlighted in the red box (which is from users table) just 1 time and all the records associated with user id in add_game table?

This is my current code :

Controller

public function login()
    {

        $data = [];
        helper(['form']);
        
        $validation =  \Config\Services::validation();

        $db = db_connect();
        $model = new LoginModel($db);
        $user = $model->login($this->request->getVar('userlogin'));

        $this->setUserSession($user[0]);
        
            echo view('templates/header', $data, $user);
            echo view('account/login', $data, $user);
            echo view('templates/footer', $data, $user);
        
        
    }


    private function setUserSession($user){
        $data = [
            'user_id' => $user['user_id'],
            'username' => $user['username'],
            'email' => $user['email'],
            'firstname' => $user['firstname'],
            'lastname' => $user['lastname'],
            'dob' => $user['dob'],
            'country' => $user['country'],
            'country_code' => $user['c_code'],
            'contact' => $user['contact'],
            'game_id' => $user['game_id'],
            'ign' => $user['ign'],
            'acc_id' => $user['acc_id'],
            'isLoggedIn' => true
        ];
        
        session()->set($data);
        return true;
    }

Model:

return $this->db->table('users')
                        ->groupStart()
                            ->where('username', $str)
                            ->orWhere('email', $str)
                        ->groupEnd()
                        ->join('add_game', 'add_game.user_id = users.user_id')
                        ->get()
                        ->getResultArray();

I have a few more tables but not yet created for now so I have only joined 1 table for the time being. What am I missing? Or do I have to loop twice? Is there a way that I just need to loop 1 time? Hope someone can help me out here. Thanks in advance guys!

Vickel
  • 7,879
  • 6
  • 35
  • 56
Dr3am3rz
  • 523
  • 1
  • 13
  • 41
  • add the table structure please, it's an independent new question and people don't know the structure – Vickel Aug 16 '20 at 18:02
  • @Vickel I have added. Sorry about that as this was posted few days ago and have not gotten any help =( I have already updated the table structure – Dr3am3rz Aug 16 '20 at 18:06
  • use ->group_by('users.user_id') – Vickel Aug 16 '20 at 18:08
  • @Vickel I have tried that already but it only returns me 1 record from users and add_game table. If let's say my add_game table have 2 records, I would like to display 2 records from add_game table and 1 record from users table instead of duplicating it 2 times for users table. – Dr3am3rz Aug 16 '20 at 18:14
  • I have also tried distinct but it still doesn't achieve what I want. – Dr3am3rz Aug 16 '20 at 18:27

1 Answers1

1

the easiest way to achieve this (display 2 records from add_game table and 1 record from users table) you need to create a foreach loop in your view, and exclude duplicated data from users table to be shown.

controller:

$data['my_data']=$this->Your_model->your_method(); // your query example
$this->load->view('your_view',$data)

view:

<?php $my_id=0;foreach($my_data as $row):?>
   <?php if($my_id!=$row->user_id):?>
     <div><?=$row->username?></div>   <!--data from table user-->
     <div><?=$row->created_at?></div> <!--data from table add_game-->
   <?php else:?>
     <div><?=$row->created_at?></div> <!--only data from table add_game-->
   <?php endif;?>
<?php $my_id=$row->user_id;endforeach;?>
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • I have updated my code in the question already. I tried using your method but I get an error message when I tried to print_r($user), it showed: Undefined variable: user – Dr3am3rz Aug 16 '20 at 18:56
  • where do you see the error? in this line: `$user = $model->login($this->request->getVar('userlogin'));`? – Vickel Aug 16 '20 at 19:28
  • This line : foreach($user as $row): in my view file – Dr3am3rz Aug 16 '20 at 19:30
  • now you have to go back step by step and debug until you find why $user is undefined. You can use print_r($vat);die; as this stops the code and you can see what you have either in your view or your browser's network tab. Once you fixed it, you'll see my example will work just fine... – Vickel Aug 16 '20 at 19:40
  • What is $vat? Apparently I have the same error message $vat is undefined too – Dr3am3rz Aug 16 '20 at 19:48
  • sorry, it should say $var and means any variable you want to inspect. As I said earlier, you need to go back in your code until you find where there error is, probably in the LoginModel? – Vickel Aug 16 '20 at 19:49
  • Alright! I will try to find out what's wrong and get back to you again! – Dr3am3rz Aug 16 '20 at 19:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219920/discussion-between-vickel-and-dr3am3rz). – Vickel Aug 16 '20 at 19:51
  • So sorry to bother you again. I am trying to figure out the code that you have showed me. I couldn't figure it out the logic. Previously I have only 2 tables joined. Now I tried to join the 3rd table, the duplicates starts again. What should I do if I have more than 2 tables and more tables joined? – Dr3am3rz Aug 30 '20 at 16:27
  • can you please post a new question? And explain the problem with as short as possible code example. thanks – Vickel Aug 30 '20 at 16:31