提问人:Kingsley Llames 提问时间:11/7/2023 更新时间:11/8/2023 访问量:27
遇到 PHP 错误严重性:警告消息:未定义的变量$poke
A PHP Error was encountered Severity: Warning Message: Undefined variable $poke
问:
遇到 PHP 错误 严重性:警告
消息:未定义的变量$poke
文件名: views/pokeview.php
行号:44
回溯:
文件:C:\xampp\htdocs\llames3\application\views\pokeview.php 行:44 功能:_error_handler
文件:C:\xampp\htdocs\llames3\application\controllers\Nav.php 行:13 功能:视图
文件: C:\xampp\htdocs\llames3\index.php 线路:315 功能:require_once
遇到未捕获的异常 类型:TypeError
消息:sizeof():参数 #1 ($value) 必须为 Countable|array 类型,给定 null
文件名: C:\xampp\htdocs\llames3\application\views\pokeview.php
行号:44
回溯:
文件:C:\xampp\htdocs\llames3\application\controllers\Nav.php 行:13 功能:视图
文件: C:\xampp\htdocs\llames3\index.php 线路:315 功能:require_once
(https://i.stack.imgur.com/ZHsx6.png)
这是我的控制器
<?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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
pokedex 数据库不会显示在我的作品中。谁能帮我调试这个?
答:
0赞
Salz
11/8/2023
#1
你正在打开索引,对吧?
你为什么不把poke变量也添加到索引方法中?
控制器代码应如下所示:
<?php
public function index() {
$pokemon = array(
'poke' => $this->Pokemodel->poketable()
);
$this->load->view('pokeview', $pokemon);
}
完整代码 :
<?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() {
// Add this line
$pokemon = array(
'poke' => $this->Pokemodel->poketable()
);
$this->load->view('pokeview', $pokemon);
}
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');
}
}
评论
$poke
isset()
$poke