I'm trying to show the results from my database in a dynamic table, I'm using codeigniter:
This is my model:
class Cliente_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function obtenerDatos() {
$data = $this->db->get('cliente');
return $data;
}
This is my controller:
class Cliente extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('cliente_model');
}
public function index()
{
$tabla = $this->cliente_model->obtenerDatos();
$data = $tabla->result_array();
print_r($data); //This works
$this->load->view('cliente_view',$data);
}
}
And this is the table part of my view:
<table class="table table-sm table-striped table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Cliente</th>
<th hidden scope="col">ID</th>
<th scope="col">Correo 1</th>
<th scope="col">Correo 2</th>
<th scope="col">Correo 3</th>
<th scope="col">Correo 4</th>
<th scope="col">Correo 5</th>
<th scope="col">Correo 6</th>
<th scope="col">Correo 7</th>
<th scope="col">Correo 8</th>
<th scope="col">Status</th>
<th scope="col">Acción</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $fila): ?>
<tr>
<th scope="row"><?php echo $fila['nombreCliente'];?></td>
<td hidden><?php echo $fila['idCliente'];?></td>
<td><?php echo $fila['correoCliente1'];?></td>
<td><?php echo $fila['correoCliente2'];?></td>
<td><?php echo $fila['correoCliente3'];?></td>
<td><?php echo $fila['correoCliente4'];?></td>
<td><?php echo $fila['correoCliente5'];?></td>
<td><?php echo $fila['correoCliente6'];?></td>
<td><?php echo $fila['correoCliente7'];?></td>
<td><?php echo $fila['correoCliente8'];?></td>
<?php if ($fila['statusCliente'] == 'Activo') { ?>
<td><span class="badge badge-pill badge-success">Activo</span></td>
<?php } else { ?>
<td><span class="badge badge-pill badge-warning">Inactivo</span></td>
<?php } ?>
<td><a href="#" title=""><span class="badge badge-pill badge-warning">Editar </span><img src="../imagenes/glyphicons/png/glyphicons-151-edit.png" alt="" title=""></a></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
When I call the print_r function in my controller the $data variable shows all the records that I want to pass to the view, but when I send to the view gives me these errors in the foreach function:
Error 1:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/cliente_view.php
Line Number: 44
Backtrace:
File: C:AppServwwwCariLMSapplicationviewscliente_view.php
Line: 44
Function: _error_handler
File: C:AppServwwwCariLMSapplicationcontrollersCliente.php
Line: 17
Function: view
File: C:AppServwwwCariLMSindex.php
Line: 315
Function: require_once
Error 2:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/cliente_view.php
Line Number: 44
Backtrace:
File: C:AppServwwwCariLMSapplicationviewscliente_view.php
Line: 44
Function: _error_handler
File: C:AppServwwwCariLMSapplicationcontrollersCliente.php
Line: 17
Function: view
File: C:AppServwwwCariLMSindex.php
Line: 315
Function: require_once
Do you have any idea why is not working?
See Question&Answers more detail:
os