1

I am trying to show display a "join" result and only want to show the user_id, username, email only once for users table even if there are many records on the other table, so I tried to make a query builder like below:

Table users                          | Table add_game
                                     |
user_id | username | email           | game_id | user_id | ign    | acc_id

1       | userA    | userA@email.com | 1       | 1       | ignA   | accA
2       | userB    | userB@gmail.com | 2       | 1       | ignB   | accB
                                     | 1       | 2       | ignB   | accB
                                     | 3       | 2       | ignD   | accD 

Model :

<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;

class LoginModel{
    
    protected $db;

    public function __construct(ConnectionInterface &$db){
        $this->db =& $db;
    }


    public function login(string $str)
    {


        return $this->db->table('users')
                        
                        ->groupStart()
                            ->where('username', $str)
                            ->orWhere('email', $str)
                        ->groupEnd()
                        
                        ->join('add_game', 'add_game.user_id = users.user_id')
                        //->distinct('users.user_id')
                        //->select(("GROUP_CONCAT(game_id, ign, acc_id) AS userdata"))
                        ->get()
                        ->getResultArray();
    }
    
}

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);
        
            echo view('templates/header', $data);
            echo view('account/login', $data);
            echo view('templates/footer', $data);
        
        
    }


    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;
    }

But right now I am getting

Undefined index: user_id

error message. Previously there was no issue or error when I was using without query builder for my login :

public function login(string $str, string $fields, array $data)
    {
        return $this->where('username', $data['userlogin'])->orWhere('email', $data['userlogin'])
                                  ->first();
    }

How to resolve this error?

Vickel
  • 7,879
  • 6
  • 35
  • 56
Dr3am3rz
  • 523
  • 1
  • 13
  • 41
  • BUT table add_game doesn't have a user_id column? at least not in your code example – Vickel Aug 16 '20 at 16:23
  • @Vickel Thanks for your help! Yes, there is a user_id column in my add_game table. The example that I am showing is the result display. I have updated the chart already. I am not sure is it there is something wrong with my query builder or what. – Dr3am3rz Aug 16 '20 at 16:51
  • what does `echo '
    ';print_r($user);die;`spit out?
    – Vickel Aug 16 '20 at 16:56
  • @Vickel Here : https://imgur.com/a/olgG8lm I am trying to show once for the red highlighted box https://i.stack.imgur.com/sUW0M.jpg So I thought of passing the array into the session and try to loop 2 times to achieve what I want but turns out it give me Undefined index: user_id error. – Dr3am3rz Aug 16 '20 at 17:06
  • use `$this->setUserSession($user[0]);` – Vickel Aug 16 '20 at 17:11
  • @Vickel Thanks it work! But now I am facing another issue. I tried to print out the session in my view file but I only get 1 record row. I used print_r(session()->get()); in my view file. Do I need to do the looping in my controller file under the setUserSession method or do I do it in my view file? – Dr3am3rz Aug 16 '20 at 17:34
  • avoid to write logic in your view files, prepare everything in controller – Vickel Aug 16 '20 at 17:40
  • @Vickel I see. Ok. I'm stuck here as CI is really new to me and my backend skills is beginner only. How do I go about doing it? – Dr3am3rz Aug 16 '20 at 17:42
  • that's topic for another question, I'd say – Vickel Aug 16 '20 at 17:52
  • @Vickel Alright! Sorry if I asked too much of you. Would you be able to help me out on this https://stackoverflow.com/questions/63418543/codeigniter-4-query-builder-join-display-only-1-time-from-first-table ? – Dr3am3rz Aug 16 '20 at 17:56

1 Answers1

1

As by your comment (image) your array looks like:

Array
(
  [0]=>Array
    (
      [user_id]=>1,
      [user_name]=>'test',
      //etc.
    )
)

You get the

Undefined index: user_id

error message, because of addressing wrongly the array while using 'user_id' => $user['user_id']

the correct way is to add the index you want to retrieve like:

$this->setUserSession($user[0]);   // where 0 can be changed to the index you pretend

now the array is flattened and 'user_id' => $user['user_id'] doesn't throw an error anymore.

Vickel
  • 7,879
  • 6
  • 35
  • 56