Show result of a query in html - PHP + CodeIgniter.

2

Hello, I am trying to make an example in codeIgniter where I want to display data from the database in html forms.

This is my query:

    $id_entidade = $this->session->userdata('id_entidade');
    $query = $this->db->query("SELECT * FROM entidade where id_entidade = '$id_entidade'");
    foreach ($query->result() as $row){
        echo $row->nome;
        echo $row->email;}

Here it is working right, now I would like to know how it shows there in html.

I tried to do this:

<?php foreach($query as $row): ?>
    <input type="text" class="form-control" name="nome" placeholder="Nome" value="<?php echo $query->nome ;?>" />
 <?php endforeach; ?>

But it's not working, could you give me tips? I'm starting the framework study, so if I'm very wrong, I'll accept improvement tips.

Thanks to whoever respond, hug.

    
asked by anonymous 04.08.2016 / 00:55

2 answers

3

Using the MVC framework, but remembering that this is an example in which it can be changed and improved:

In model you do this:

class Entidade_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }
    public function getEntidade($id_entidade){
        $query = $this->db->query("SELECT * FROM entidade where id_entidade = ? ", array($id_entidade))->result();
        return $query;
        }
} 

controller You make a call to the model:

public function exibeResultado(){
     $this->load->model('entidade_model');
     $dados['resultados'] = $this->entidade_model->getEntidade($this->session->userdata('id_entidade'));
     $this->load->view('sua_view', $dados);
}

In your view you do this:

<?php foreach ($resultados as $resultado): ?>
    <tr>
       <td><?php echo $resultado->nomeColunaBd1; ?></td>
       <td><?php echo $resultado->nomeColunaBd2; ?></td>
    </tr>
<?php endforeach; ?>
    
05.08.2016 / 14:42
1

You can build your function as follows

# Consulta
$this->db->where("id_entidade", $this->session->userdata('id_entidade'));
$results = $this->db->get('entidade')->result();        

# foreach de retorno
foreach($results as $valor){
    echo '<input type="text" class="form-control" name="nome" placeholder="Nome" value="{$valor->nome}" />';
}

But remembering that the correct method is to transpose this between the methods of views, controllers and models.

    
04.08.2016 / 01:00