无法在视图Codeigniter中显示查询结果 [英] unable to display query results in a view codeigniter

查看:76
本文介绍了无法在视图Codeigniter中显示查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在动态表中显示数据库的结果,我正在使用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

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

解决方案

在模型函数中写入以下内容:

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

这在您的控制器中:

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

然后将其添加到您的视图中:

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

等...

正如您将看到的,$data['client']现在正在保存您的数据库信息,您可以通过在视图中循环访问关联数组来读取它.有关查询生成器类的详细信息,请参见此处.

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:\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

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:\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

Do you have any idea why is not working?

解决方案

Write this in your model function:

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

And this in your controller:

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

Then add this to your view:

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

etc...

As you will see, $data['client'] is now holding your database info, and you can read it by looping through the associative array in your view. More info on query builder class here.

这篇关于无法在视图Codeigniter中显示查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆