A PHP Error was encountered Severity: Warning
Message: Undefined variable $poke
Filename: views/pokeview.php
Line Number: 44
Backtrace:
File: C:\xampp\htdocs\llames3\application\views\pokeview.php Line: 44 Function: _error_handler
File: C:\xampp\htdocs\llames3\application\controllers\Nav.php Line: 13 Function: view
File: C:\xampp\htdocs\llames3\index.php Line: 315 Function: require_once
An uncaught Exception was encountered Type: TypeError
Message: sizeof(): Argument #1 ($value) must be of type Countable|array, null given
Filename: C:\xampp\htdocs\llames3\application\views\pokeview.php
Line Number: 44
Backtrace:
File: C:\xampp\htdocs\llames3\application\controllers\Nav.php Line: 13 Function: view
File: C:\xampp\htdocs\llames3\index.php Line: 315 Function: require_once
(https://i.sstatic.net/ZHsx6.png)
here is my controller
<?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');
}
}
?>
model
<?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');
}
}
?>
view
<!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>
The pokedex database won't show in my work. Can anyone help me debug this one?
You're opening index right?
why didn't you add poke variable to index method too?
Controller code should be like this :
<?php
public function index() {
$pokemon = array(
'poke' => $this->Pokemodel->poketable()
);
$this->load->view('pokeview', $pokemon);
}
Full code :
<?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');
}
}