unable to display query results in a view codeigniter

前端 未结 2 1303
臣服心动
臣服心动 2021-01-22 16:47

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 {

fu         


        
相关标签:
2条回答
  • 2021-01-22 17:34

    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.

    0 讨论(0)
  • 2021-01-22 17:54

    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.

    0 讨论(0)
提交回复
热议问题