Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

php - unable to display query results in a view codeigniter

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...