Reputation: 75
I am new to codeingiter and searched everywhere for a fix on this but nothings working for me yet the issue is so simple.. i want to display my database records in a list or table and make them available to click/ use etc.. but i keep getting an undefined variable error for query (which i thought was defined) and an invalid argument supplied foreach error.. any help will be massively appreciated
Controller
public function viewtemplate()
{
$this->load->model('Template_model');
$data['query'] = $this->Template_model->viewtemplate();
$this->load->view('dashboard', $data);
}
Model
function viewtemplate()
{
$query = $this->db->get('users');
return $query->result() ;
}
View
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->idtemplates; ?></td>
<td><?php echo $row->templatestyle ?></td>
<td><?php echo $row->users_temp_id; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Error 1
Severity: Notice
Message: Undefined variable: query
Filename: views/dashboard.php
Line Number: 154
Error 2
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/dashboard.php
Line Number: 154
Upvotes: 0
Views: 87
Reputation: 11
first, you have to write the semicolon in
<td><?php echo $row->templatestyle ?></td> as
<td><?php echo $row->templatestyle; ?></td>
then try to change the variable $query
in your controller.
Upvotes: 0
Reputation: 38
Add in the view before the foreach the following to see if your controller is sending any data to the view:
<?php echo var_dump($query);?>
if the variable $query is not an array thats why you are getting an error inside the foreach, since is trying to use a property that doesnt exist.
Edit:
So the real problem was the code was on another controller function and he was seeing the index of the controller, where the query variable wasnt defined.
Upvotes: 0