Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 ErrorException Undefined variable: table
#1

I am facing this issue of Undefined variable as shown in the image attached which I am not sure what is wrong.

[Image: LOPyi.jpg]

My code as follows:

Controller


Code:
public function game_reg()
    {
        $data = [];
        helper(['form']);
       
        $validation =  \Config\Services::validation();
       
        if($this->request->getMethod() == 'post'){
           
            $user_id = session()->get('user_id');
           
            //validations
           
            $rules = [
                'game_id' => 'required',
                'ign' => 'required|is_unique[game_reg.ign]',
                'acc_id' => 'required'
            ];
           
            $errors = [
                'ign' => [
                    'is_unique' => 'IGN already exist!'
                ],
                'acc_id' => [
                    'is_unique' => 'Account ID already exist!'
                ]
            ];
           
           
            if(!$this->validate($rules, $errors)){
                $data['validation'] = $this->validator;
            }else{
                //store information into database
               
                $model = new GameregModel();
               
                $newData = [
                    'game_id' => $this->request->getVar('game_id'),
                    'ign' => $this->request->getVar('ign'),
                    'acc_id' => $this->request->getVar('acc_id'),
                    'user_id' => session()->get('user_id'),
                    'created_by' => session()->get('username')
                ];
                $model->save($newData);
                $session = session();
                $session->setFlashdata('success', 'Game Successfully Added!');
                return redirect()->to(base_url('account/game_reg'));
               
            }
        }
           
       
       
        echo view('templates/header', $data);
        echo view('account/game_reg');
        echo view('templates/footer');
    }

Views


Code:
<select id="myDropdown">
               
                    <?php
                       
                        $i = 1;
                        foreach($table as $t) :
                        $i++;
                       
                    ?>
                   
                    <option value="<?= $t['game_id']; ?>" data-imagesrc="/img/logo_<?= strtolower($t['game_name']); ?>.jpg"><?= $t['game_name']; ?></option>
                   
                    <?php endforeach; ?>
                   
                </select>


If I remove away the is_unique function, everything works perfectly fine but when I include the is_unique, I get the error. Hope someone can help me out of this. Thanks in advance guys!
Reply
#2

Your $data array dosen't contain anything named "table".

$data['table'] = [];
Reply




Theme © iAndrew 2016 - Forum software by © MyBB