错误异常:CodeIgniter 4中未定义的变量

qaxu7uf2  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(151)

我在read_hunter.php文件的第27行遇到了ErrorException问题。
错误如下:<?php foreach($hunter as $hxh): ?>中的Undefined variable $hunter
我不明白为什么会发生这种情况,因为无法可视化MySQL Workbench数据库表中的记录数。

  • read_hunter.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/9326dfce3d.js" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>Read hunters</title>
</head>
<body>
    <table class="table">
        <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">Name</th>
                <th scope="col">Year</th>
                <th scope="col">Height</th>
                <th scope="col">Weight</th>
                <th scope="col">Type Hunter</th>
                <th scope="col">Type Nen</th>
                <th scope="col">Type Blood</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($hunter as $hxh): ?> // Erro here of variable undefined
                <th>
                    <td><?php echo['id_hunter']?></td>
                    <td><?php echo['name_hunter']?></td>
                    <td><?php echo['year_hunter']?></td>
                    <td><?php echo['height_hunter']?></td>
                    <td><?php echo['weight_hunter']?></td>
                    <td><?php echo['type_hunter']?></td>
                    <td><?php echo['type_nen']?></td>
                    <td><?php echo['type_blood']?></td>
                    <td>Update | Delete </td>
                </th>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php echo $pager->links();?>
</body>
</html>
  • HunterModel.php
<?php

namespace App\Models;

use CodeIgniter\Model;

class HunterModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'hunter';
    protected $primaryKey       = 'id_hunter';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'name_hunter',
        'year_hunter',
        'height_hunter',
        'weight_hunter',
        'type_hunter',
        'type_nen',
        'type_blood'
    ];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
}
  • HunterController.php
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\HunterModel;

class HunterController extends BaseController
{
    private $hunter;

    public function __construct()
    {
        $this->hunter = new HunterModel();
    }

    public function readHunters()
    {
        return view('read_hunter', [
            'read_hunter' => $this->hunter->paginate(10),
            'pager' => $this->hunter->pager
        ]);
    }
}
oxosxuxt

oxosxuxt1#

您只在控制器中声明了$hunter,但没有在视图中发送

public function readHunters()
{
    return view('read_hunter', [
        'hunter' => $this->hunter,
        'read_hunter' => $this->hunter->paginate(10),
        'pager' => $this->hunter->pager
    ]);
}
hyrbngr7

hyrbngr72#

您调用了错误的变量$hunter,它应该是$read_hunter
并且您在<td>中的echo完全不正确。

<tbody>
            <?php foreach($read_hunter as $hxh): ?> // no error anymore
                <th>
                    <td><?php echo $hxh['id_hunter']?></td>
                    <td><?php echo $hxh['name_hunter']?></td>
                    <td><?php echo $hxh['year_hunter']?></td>
                    <td><?php echo $hxh['height_hunter']?></td>
                    <td><?php echo $hxh['weight_hunter']?></td>
                    <td><?php echo $hxh['type_hunter']?></td>
                    <td><?php echo $hxh['type_nen']?></td>
                    <td><?php echo $hxh['type_blood']?></td>
                    <td>Update | Delete </td>
                </th>
            <?php endforeach; ?>
        </tbody>

相关问题