0
Someone could help with this mistake?
In BD I have the table Cities and I’m trying to list cities, I’ve made several changes and it doesn’t work.
When listing cities I will improve the query, I have more tables with other information that peecisam be displayed.
Below are project files.
Error message:
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>
Libaries: File: D: BF meusite application Libraries Template.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $ci;
function __construct() {
$this->ci =& get_instance();
}
function load($tipo_view, $pagina = null, $data = null, $template = 'default.php') {
if ( ! is_null( $pagina ) ) {
if ( file_exists( APPPATH.'views/frontend/'.$tipo_view.'/'.$pagina ) ) {
$body_view_path = 'frontend/'.$tipo_view.'/'.$pagina;
}
else if ( file_exists( APPPATH.'views/frontend/'.$tipo_view.'/'.$pagina.'.php' ) ) {
$body_view_path = 'frontend/'.$tipo_view.'/'.$pagina.'.php';
}
else if ( file_exists( APPPATH.'views/frontend/'.$pagina ) ) {
$body_view_path = 'frontend/'.$pagina;
}
else if ( file_exists( APPPATH.'views/frontend/'.$pagina.'.php' ) ) {
$body_view_path = 'frontend/'.$pagina.'.php';
}
else {
show_error('Unable to load the requested file: frontend/' . $tipo_view.'/'.$pagina.'.php');
}
$body = $this->ci->load->view($body_view_path, $data, TRUE);
if ( is_null($data) ) {
$data = array('body' => $body);
}
else if ( is_array($data) ) {
$data['body'] = $body;
}
else if ( is_object($data) ) {
$data->body = $body;
}
}
$this->ci->load->view('frontend/templates/'.$template, $data);
}
function load_admin($tipo_view, $pagina = null, $data = null, $template = 'default.php') {
if ( ! is_null( $pagina ) ) {
if ( file_exists( APPPATH.'views/admin/'.$tipo_view.'/'.$pagina ) ) {
$body_view_path = 'admin/'.$tipo_view.'/'.$pagina;
}
else if ( file_exists( APPPATH.'views/admin/'.$tipo_view.'/'.$pagina.'.php' ) ) {
$body_view_path = 'admin/'.$tipo_view.'/'.$pagina.'.php';
}
else if ( file_exists( APPPATH.'views/admin/'.$pagina ) ) {
$body_view_path = 'admin/'.$pagina;
}
else if ( file_exists( APPPATH.'views/admin/'.$pagina.'.php' ) ) {
$body_view_path = 'admin/'.$pagina.'.php';
}
else {
show_error('Unable to load the requested file: admin/' . $tipo_view.'/'.$pagina.'.php');
}
$body = $this->ci->load->view($body_view_path, $data, TRUE);
if ( is_null($data) ) {
$data = array('body' => $body);
}
else if ( is_array($data) ) {
$data['body'] = $body;
}
else if ( is_object($data) ) {
$data->body = $body;
}
}
$this->ci->load->view('admin/templates/'.$template, $data);
}
function load_admin_login($data = array()) {
$this->ci->load->view('admin\templates\login.php', $data);
}
function load_from_db($tipo_view, $pagina = null, $data = null, $template = 'default.php') {
if ( ! is_null( $pagina ) ) {
$CI =& get_instance();
$CI->db->select('corpo, titulo');
$query = $CI->db->get_where('paginas', array('url' => $pagina))->result_array();
if (empty($query)) {
show_error('Unable to load the requested file: '.$pagina);
} else {
$body = $query[0]['corpo'];
$titulo = $query[0]['titulo'];
}
if ( is_null($data) ) {
$data = array('body' => $body);
}
else if ( is_array($data) ) {
$data['body'] = $body;
$data['title'] .= $titulo;
}
else if ( is_object($data) ) {
$data->body = $body;
}
}
$this->ci->load->view('frontend/templates/'.$template, $data);
}
}
Controller: D: BF meusite application controllers Pages.php
<?php
defined('BASEPATH') OR Exit('No direct script access allowed');
class Pages extends Ci_controller {
public function view($page = 'home') {
$this->load->model('menus_model');
$data = array();
$data['base_url'] = base_url();
$data['title'] = $page;
if ($page == "home") {
$data['title'] = 'Home';
}
$data['menus'] = $this->menus_model->getMenus();
$data['menu_ativo'] = $page;
$this->load->model('paginas_model');
$data['events'] = array();
$data['header'] = $this->load->view('frontend/includes/header.php', $data, true);
$data['navigation'] = $this->load->view('frontend/includes/navigation.php', $data, true);
$data['footer'] = $this->load->view('frontend/includes/footer.php', $data, true);
if (file_exists(APPPATH.'views/frontend/pages/'.$page.'.php')) {
// Existe uma view com esse nome, vamos carregá-la
$this->template->load('pages', $page, $data,'default');
} else {
if ($this->paginas_model->paginaExisteNoDb($page) == true) {
// Existe essa página no banco de dados, vamos carregá-la
$data['menu_ativo'] = $this->menus_model->getPageMenuAtivo($page);
$this->template->load_from_db('pages', $page, $data, 'default');
} else {
// Não exite nem view, nem no banco. Erro 404.
$this->template->load('pages', 'not_found', $data, 'default');
}
}
}
}
Dear Gustado, I don’t think that’s what it is, because if it was, it would have worked with
$dados = array('Buscar' => $lista);
, That’s not var name$dados["Buscar"]
, it’s a way forced to make php generate an array that would be the same as$dados = array( "Buscar" => array('Buscar' => $lista) );

, in case you created an array within another... CI probably uses Extract() to generate the vars in views, so the problem is either the scope within the view, probably it is using "sections" or some "include" from another view. So his sub-view has no way to get the value.– Guilherme Nascimento
yes, I noticed now that he is doing something strange . php $this->load->view('blogview', $data);
– Gustavo Castro
Dear Gustavo, there is no blogview in his code. His syntax seems correct, it is more likely that it is the scope of the variable even.
– Guilherme Nascimento