Codeigniter 4 ErrorException 未定义变量:table

Codeigniter 4 ErrorException Undefined variable: table

我遇到了这个未定义变量的问题,如附图所示,我不确定哪里出了问题。

我的代码如下:

路线

$routes->get('account/game_reg', 'Game::index');
$routes->match(['get', 'post'], 'account/game_reg', 'Game::game_reg');

控制器

public function index()
    {
        $data = [];
        
        if(!session()->get('isLoggedIn')):
            return redirect()->to(base_url('account/login'));
        endif;
        
        $games = new GamelistModel();
        
        $data['table'] = $games->getList();
        
        echo view('templates/header', $data);
        echo view('account/game_reg');
        echo view('templates/footer');
    }

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

GameregModel

<?php namespace App\Models;

use CodeIgniter\Model;

class GameregModel extends Model{
    
    protected $table = 'game_reg';
    protected $allowedFields = [
        'user_id',
        'game_id',
        'ign',
        'acc_id',
        'created_at',
        'updated_at',
        'created_by'
    ];
    
    
}

?>

游戏列表模型

<?php namespace App\Models;

use CodeIgniter\Model;

class GamelistModel extends Model{
    
    protected $table = 'game_list';
    protected $primarykey = 'game_id';
    protected $allowedFields = [
        'game_id',
        'game_name'
    ];
    
    public function getList()
    {
        return $this->orderBy('game_id', 'ASC')->findAll();
    }
    
}

?>

观看次数

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

如果我删除 is_unique 函数,一切正常,但是当我包含 is_unique 时,我得到错误。我想做的是,我会检索管理员更新的游戏列表,然后用户将从该列表中选择并保存到他们的个人资料中。

希望有人能帮我解决这个问题。

提前谢谢大家!

您只是将数据发送到您的一个视图中。使用 $table 变量的人无法访问它。

因此,在您的加载视图代码中,您必须执行以下操作:

 echo view('templates/header', $data);
 echo view('account/game_reg', $data);
 echo view('templates/footer', $data);

最好只在所有视图中发送数据。

我注意到的另一件事(与此处的问题无关)是您关闭了模型中的 php 标签。永远不要那样做。您的 类 永远不应包含 php 结束标记 ?>.