Reputation: 1828
I am still new at PHP and the MVC concept. I have been trying to duplicate the CI News Tutorial(http://codeigniter.com/user_guide/tutorial/news_section.html), while using my own database and rewriting the code.
I have been unsuccessful.
Here is my controller:
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
$this->load->model('structures_model');
}
public function structures()
{
$this->load->model('structures_model');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['structures_all'] = $this->structures_model->get_structures();
$this->load->view('templates/header', $data);
$this->load->view('templates/navmain', $data);
$this->load->view('structures', $data);
$this->load->view('templates/footer', $data);
}
}
Here is my model (structures_model)
<?php
class Structures_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_structures()
{
$query = $this->db->get('structures');
return $query->result_array();
}
}
And my view:
<?php foreach ($structures_all as $structures_info): ?>
<h2>Structures</h2>
<div id="main">
<?php echo $structures_info['str_name'] ?>
</div>
<?php endforeach ?>
The error im getting is the common :
A PHP Error was encountered<
Severity: Notice
Message: Undefined variable: structures_all
Filename: main/structures.php
Line Number: 2
I am at a loss. I have looked at all the similar errors people have gotten, but can't figure out why exactly the structure_all array is not being defined. Shouldn't it get created in the controller function where I set :
$data['structures_all'] = $this->structures_model->get_structures();
What am I missing?
Upvotes: 0
Views: 4334
Reputation: 7388
The best way to debug this would be to assign $data['structures_all']
a definite array value, say: $data['structures_all'] = array('foo' => 'bar');
Is the $structures_all
variable available in the view now? If it is available, you know that $this->structures_model->get_structures();
is returning null
.
Do you have a table in your database called structures
?
Are you sure your database connection details are filled out in config/database.php
?
Do you have php error reporting set to all? There might be hidden messages... call error_reporting(E_ALL);
in the constructor of your controller.
Also try echoing: $this->db->last_query();
to verify your query is being constructed the same way you tried in phpmyadmin...
Hopefully this puts you on the right track.
Upvotes: 1