【发布时间】:2018-09-26 12:17:09
【问题描述】:
我正在尝试在动态表中显示我的数据库中的结果,我正在使用 codeigniter:
这是我的模型:
class Cliente_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function obtenerDatos() {
$data = $this->db->get('cliente');
return $data;
}
这是我的控制器:
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);
}
}
这是我视图的表格部分:
<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>
当我在控制器中调用 print_r 函数时,$data 变量会显示我想要传递给视图的所有记录,但是当我发送到视图时,我会在 foreach 函数中给出这些错误:
错误 1:
遇到 PHP 错误
严重性:通知消息:未定义变量:数据
文件名:views/cliente_view.php行号:44
回溯:
文件:C:\AppServ\www\CariLMS\application\views\cliente_view.php
线路:44
函数:_error_handler文件:C:\AppServ\www\CariLMS\application\controllers\Cliente.php
线路:17
功能:查看文件:C:\AppServ\www\CariLMS\index.php
线路:315
函数:require_once
错误 2:
遇到 PHP 错误
严重性:警告消息:为 foreach() 提供的参数无效
文件名:views/cliente_view.php
行号:44
回溯:
文件:C:\AppServ\www\CariLMS\application\views\cliente_view.php
线路:44
函数:_error_handler文件:C:\AppServ\www\CariLMS\application\controllers\Cliente.php
线路:17
功能:查看文件:C:\AppServ\www\CariLMS\index.php
线路:315
函数:require_once
你知道为什么不工作吗?
【问题讨论】:
-
这是第 44 行:
-
print_r($data); // This works- 告诉我们结果是什么。 -
Array ( [0] => Array ( [idCliente] => 1 [nombreCliente] => Rassini [correoCliente1] => pahoran_01@hotmail.com [correoCliente2] => [correoCliente3] => [ correoCliente4] => [correoCliente5] => [correoCliente6] => [correoCliente7] => [correoCliente8] => [statusCliente] => Activo ) [1] => 数组 ( [idCliente] => 2 [nombreCliente] => Rassini 3 [correoCliente1] => pahoran_01@gmail.com [correoCliente2] => pahoranalanis@gmail.com [correoCliente3] => [correoCliente4] => [correoCliente5] => [correoCliente6] => [correoCliente7] => [correoCliente8] = > [statusCliente] => 不活跃 ) )
-
请编辑您的问题以包含所有信息。它在 cmets 中非常难以阅读。
标签: php mysql codeigniter