简体   繁体   中英

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

If you call a view with a data array, the provided array needs to be an associative array, that uses the variable names as keys and its values as the variable values. Example:

//Controller
$data = [];
$data['var1'] = true;
$data['var2'] = 'This is a string value';
$this->load->view('cliente_view',$data);

Now, you are able to call the variables $var1 and $var2 in your view, because the are keys in the data array

//View
if ($var1)
    echo $var2;
else
    echo 'false';

So the solution for your problem is to add the variable name 'data' as a key to your provided array:

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

After that you can use the variable $data in your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM