gpt4 book ai didi

php - 无法在 View codeigniter 中显示查询结果

转载 作者:行者123 更新时间:2023-11-29 01:19:40 27 4
gpt4 key购买 nike

我正在尝试在动态表中显示我的数据库的结果,我正在使用 codeigniter:

这是我的模型:

class Cliente_model extends CI_Model {

function __construct() {
parent::__construct();
}

public function obtenerDatos() {

$data = $this->db->get('cliente');
return $data;
}

这是我的 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);
}
}

这是我 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>

当我在我的 Controller 中调用 print_r 函数时,$data 变量显示了我想传递给 View 的所有记录,但是当我发送给 View 时,foreach 函数中出现了这些错误:

错误一:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: data
Filename: views/cliente_view.php

Line Number: 44

Backtrace:

File: C:\AppServ\www\CariLMS\application\views\cliente_view.php
Line: 44
Function: _error_handler

File: C:\AppServ\www\CariLMS\application\controllers\Cliente.php
Line: 17
Function: view

File: C:\AppServ\www\CariLMS\index.php
Line: 315
Function: require_once

错误 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:\AppServ\www\CariLMS\application\views\cliente_view.php
Line: 44
Function: _error_handler

File: C:\AppServ\www\CariLMS\application\controllers\Cliente.php
Line: 17
Function: view

File: C:\AppServ\www\CariLMS\index.php
Line: 315
Function: require_once

你知道为什么不起作用吗?

最佳答案

在你的模型函数中写下:

$query = $this->db->get('cliente');
$data = $query->result_array();
return $data;

这在你的 Controller 中:

$data['client']= $this->cliente_model->obtenerDatos();
$this->load->view('cliente_view',$data);

然后将其添加到您的 View 中:

<?php if ($client): foreach($client as $fila):?>
<td><?php echo $fila['correoCliente1'];?></td>

等...

正如您将看到的,$data['client'] 现在保存着您的数据库信息,您可以通过在 View 中遍历关联数组来读取它。有关查询构建器类的更多信息 here .

关于php - 无法在 View codeigniter 中显示查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com