CodeIgniter Forums
Codeigniter 4 ErrorException Undefined variable: table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Codeigniter 4 ErrorException Undefined variable: table (/showthread.php?tid=77169)



Codeigniter 4 ErrorException Undefined variable: table - Dr3am3rz - 07-28-2020

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!


RE: Codeigniter 4 ErrorException Undefined variable: table - jreklund - 08-01-2020

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

$data['table'] = [];