CodeIgniter框架Pokedex页面报错请求调试:变量未定义与类型错误
错误信息
- PHP警告:未定义变量
$poke,文件views/pokeview.php行44 - TypeError异常:
sizeof()参数#1必须为Countable|array类型,传入了null,文件views/pokeview.php行44
回溯路径:
文件: C:\xampp\htdocs\llames3\application\views\pokeview.php 行号: 44 函数: _error_handler
文件: C:\xampp\htdocs\llames3\application\controllers\Nav.php 行号: 13 函数: view
文件: C:\xampp\htdocs\llames3\index.php 行号: 315 函数: require_once
控制器代码
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Nav extends CI_Controller{ public function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->model('Pokemodel'); } public function index(){ $this->load->view('pokeview'); } public function pokemonTable() { $pokemon = array( 'poke' => $this->Pokemodel->poketable() ); $this->load->view('pokeview', $pokemon); } public function insertPokemon(){ $this->load->view('insert_form'); } public function pokemonInsert() { $pokeName = $this->input->post('pokeName'); $typeOne = $this->input->post('typeOne'); $typeTwo = $this->input->post('typeTwo'); $this->load->model('Pokemodel'); $this->Pokemodel->pokeinsert($pokeName, $typeOne, $typeTwo); $data['poke'] = $this->Pokemodel->poketable(); $this->load->view('pokeview', $data); } public function searchPokemon(){ $searchTerm = $this->input->get('search'); $this->load->model('Pokemodel'); $data['poke'] = $this->Pokemodel->searchPokemon($searchTerm); $this->load->view('pokeview', $data); } public function viewPokemon($pokeName) { $data['pokemon'] = $this->Pokemodel->get_pokemon_by_name($pokeName); $this->load->view('view_pokemon', $data); } public function deletePokemon($pokeName) { $this->Pokemodel->delete_pokemon($pokeName); redirect('Nav/pokemonTable'); } } ?>
模型代码
<?php class Pokemodel extends CI_Model{ public function poketable(){ $q = $this->db->query("SELECT * FROM pokedex ORDER BY pokeName ASC"); return $q->result(); } public function pokeinsert($pokeName, $typeOne, $typeTwo) { $d = array( 'pokeName' => $pokeName, 'typeOne' => $typeOne, 'typeTwo' => $typeTwo ); $this->db->insert('pokedex', $d); } public function searchPokemon($searchTerm){ if ($searchTerm !== null) { $this->db->like('pokeName', $searchTerm); $this->db->or_like('typeOne', $searchTerm); $this->db->or_like('typeTwo', $searchTerm); } $q = $this->db->get('pokedex'); return $q->result(); } public function get_pokemon_by_name($pokeName){ $q = $this->db->get_where('pokedex', array('pokeName' => $pokeName)); return $q->row(); } public function delete_pokemon($pokeName){ $this->db->where('pokeName', $pokeName); $this->db->delete('pokedex'); } } ?>
视图代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script> <title>Pokemon</title> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="http://localhost/llames3/index.php/Nav/pokemonTable">Pokedex</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="http://localhost/llames3/index.php/Nav/pokemonTable">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="http://localhost/llames3/index.php/Nav/insertPokemon">Insert</a> </li> </ul> <form class="d-flex" action="http://localhost/llames3/index.php/Nav/searchPokemon" method="get"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" name="search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav> </head> <body> <div class="container"> <table class="table table-striped"> <thead> <tr> <th scope="col">Pokemon Name</th> <th scope="col">Type One</th> <th scope="col">Type Two</th> </tr> </thead> <tbody> <?php for($i = 0; $i < sizeof($poke); $i++) {?> <tr> <th scope="row"><?php echo $poke[$i]->pokeName; ?></th> <td><?php echo $poke[$i]->typeOne; ?></td> <td><?php echo $poke[$i]->typeTwo; ?></td> <td> <a href="<?= base_url('index.php/Nav/viewPokemon/' . $poke[$i]->pokeName) ?>" class="view-btn" >View</a> <a href="<?= base_url('index.php/Nav/deletePokemon/' . $poke[$i]->pokeName) ?>" class="delete-btn">Delete</a> </td> </tr> <?php } ?> </tbody> </table> </div> </body> </html>
调试方案
1. 修复控制器index方法未传数据问题
当前index方法直接加载视图,未传递$poke变量,导致视图报错。修改方式二选一:
// 方案1:重定向到已正确传参的pokemonTable方法 public function index(){ redirect('Nav/pokemonTable'); } // 方案2:直接获取数据并传递给视图 public function index(){ $data['poke'] = $this->Pokemodel->poketable(); $this->load->view('pokeview', $data); }
2. 视图中增加变量安全检查
在视图的循环逻辑前,先确保$poke存在且为可计数类型,避免sizeof报错:
<tbody> <?php // 初始化变量,避免未定义警告 $poke = isset($poke) ? $poke : []; if(is_countable($poke) && sizeof($poke) > 0) { for($i = 0; $i < sizeof($poke); $i++) {?> <tr> <th scope="row"><?php echo $poke[$i]->pokeName; ?></th> <td><?php echo $poke[$i]->typeOne; ?></td> <td><?php echo $poke[$i]->typeTwo; ?></td> <td> <a href="<?= base_url('index.php/Nav/viewPokemon/' . $poke[$i]->pokeName) ?>" class="view-btn" >View</a> <a href="<?= base_url('index.php/Nav/deletePokemon/' . $poke[$i]->pokeName) ?>" class="delete-btn">Delete</a> </td> </tr> <?php } } else { ?> <tr><td colspan="4">暂无宝可梦数据</td></tr> <?php } ?> </tbody>
3. 验证数据库连接与查询结果
检查application/config/database.php中的数据库配置是否正确,可在模型poketable方法中添加调试代码确认查询状态:
public function poketable(){ $q = $this->db->query("SELECT * FROM pokedex ORDER BY pokeName ASC"); // 调试用:打印执行的SQL语句和结果行数 // echo $this->db->last_query(); // echo '<br>结果行数:'.$q->num_rows(); return $q->result(); }
内容的提问来源于stack exchange,提问作者Kingsley Llames
相关产品推荐
相关产品推荐

