Estou tentando listar cidades que estão na tabela cities do meu BD e ocorre o erro abaixo:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: Buscar
Filename: pages/busca.php
Line Number: 31
Backtrace:
File: D:\BF\meusite\application\views\frontend\pages\busca.php
Line: 31
Function: _error_handler
File: D:\BF\meusite\application\libraries\Template.php
Line: 29
Function: view
File: D:\BF\meusite\application\controllers\Pages.php
Line: 37
Function: load
File: D:\BF\meusite\index.php
Line: 322
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/busca.php
Line Number: 31
Backtrace:
File: D:\BF\meusite\application\views\frontend\pages\busca.php
Line: 31
Function: _error_handler
File: D:\BF\meusite\application\libraries\Template.php
Line: 29
Function: view
File: D:\BF\meusite\application\controllers\Pages.php
Line: 37
Function: load
File: D:\BF\meusite\index.php
Line: 322
Function: require_once
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Buscar extends CI_Controller {
public $data = array();
public function __construct(){
parent::__construct();
$this->load->model('menus_model');
$this->data['menus'] = $this->menus_model->getMenus();
$this->data['menu_ativo'] = '';
$this->data['header'] = $this->load->view('frontend/includes/header.php', $this->data, true);
$this->data['navigation'] = $this->load->view('frontend/includes/navigation.php', $this->data, true);
$this->data['footer'] = $this->load->view('frontend/includes/footer.php', $this->data, true);
}
public function index() {
$this->load->model("Buscar_model");
$lista = $this->Buscar_model-> buscaTodos();
$dados = array('Buscar' => $lista);
$this->load->view('frontend/pages/busca.php', $dados);
}
}
Model
<?php
class Buscar_model extends CI_Model{
public function buscaTodos(){
$this->load->database(); //carrego a library de banco de dados
return $this->db->get('cities')->result_array(); //retorna os nossos dados
}
}
View
<section class="section">
<div class="categories-body">
<div class="container">
<table class="table">
<tr>
<th>Nome</th>
<th>Descrição</th>
</tr>
<?php foreach ($Buscar as $busca) : ?>
<tr>
<td><?= $busca['city_name'] ?> </td>
<td><?= $busca['state_uf'] ?> </td>
</tr>
<?php endforeach ?>
</table>