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

Asked

Viewed 1,766 times

2

Hello, I’m trying to make an example in codeIgniter where I want to show database data in html Formularios.

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 to show it in html.

I tried to do so:

<?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 accept improvement tips.

Thank you to whom you answer, hug.

2 answers

4


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

In the model Voce:

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;
        }
} 

In the controller Voce calls 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 his view you do so:

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

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.