I get the following error message and I have not been able to solve it.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: students
Filename: views/vEstudiante.php
Line Number: 85
Backtrace:
File: C:\xampp\htdocs\stu\application\views\vEstudiante.php
Line: 85
Function: _error_handler
File: C:\xampp\htdocs\stu\application\controllers\cEstudiante.php
Line: 19
Function: view
File: C:\xampp\htdocs\stu\index.php
Line: 315
Function: require_once
I have the following in the controller
class cEstudiante extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('mEstudiante'); //esta es la linea 19 del error
}
public function index(){
$this->load->view('vEstudiante');
$data['students'] = $this->mEstudiante->getAll();
$this->load->view('vEstudiante',$data);
}
public function import()
{
if(!$this->input->is_ajax_request()){
show_404();
}
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
$this->form_validation->set_rules('file','File','callback_notEmpty');
$response= false;
if(!$this->form_validation->run()){
$response['status'] = 'form-incomplete';
$response['errors'] = array(
array(
'field' => 'input[name="file"]',
'error' => form_error('file')
)
);
}
else{
try{
$filename = $_FILES["file"]["tmp_name"];
if($_FILES['file']['size'] > 0)
{
$file = fopen($filename,"r");
$is_header_removed = FALSE;
while(($importdata = fgetcsv($file, 10000, ",")) !== FALSE)
{
if(!$is_header_removed){
$is_header_removed = TRUE;
continue;
}
$row = array(
'nombre_es' => !empty($importdata[0])?$importdata[0]:'',
'apellido_pa' => !empty($importdata[1])?$importdata[1]:'',
'apellido_ma' => !empty($importdata[2])?$importdata[2]:''
);
$this->db->trans_begin();
$this->students_tbl_model->add($row);
if(!$this->db->trans_status()){
$this->db->trans_rollback();
$response['status']='error';
$response['message']='Something went wrong while saving your data';
break;
}else{
$this->db->trans_commit();
$response['status']='success';
$response['message']='Successfully added new record.';
}
}
fclose($file);
}
}
catch(Exception $e){
$this->db->trans_rollback();
$response['status']='error';
$response['message']='Something went wrong while trying to communicate with the server.';
}
}
echo json_encode($response);
}
public function notEmpty(){
if(!empty($_FILES['file']['name'])){
return TRUE;
}
else{
$this->form_validation->set_message('notEmpty','The {field} field can not be empty.');
return FALSE;
}
}
}
?>
In the view the error that marks me is that of the foreach
<div class="row">
<div class="col-xs-12">
<table class="table table-striped col-sm-12">
<thead>
<tr>
<td>Nombre</td>
<td>Apellido Pat.</td>
<td>Apellido Mat.</td>
</tr>
</thead>
<tbody>
<?php foreach((array)$students as $student): ?>
<tr>
<td><?= $student['nombre_es']?></td>
<td><?= $student['apellido_pa']?></td>
<td><?= $student['apellido_ma']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>