我在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
]);
}
}
2条答案
按热度按时间oxosxuxt1#
您只在控制器中声明了
$hunter
,但没有在视图中发送hyrbngr72#
您调用了错误的变量
$hunter
,它应该是$read_hunter
。并且您在
<td>
中的echo完全不正确。