Undefined variable in Codeigniter

Asked

Viewed 439 times

0

I am learning Codeigniter and trying to pass an array to the View through my controller and when I call this array in the view is presented to me as undefined.

This is my controller:

<?php 
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Consulta extends CI_Controller {
        public function index() {
            $this->load->view("index");
        }
        public function listar_consultas() {
            $this->load->view("listar");
        }

        public function salvar_consulta() {
            $this->load->model("model_marcar_consulta");
            if ($this->model_marcar_consulta->salvar_dados()) {
                $this->load->view("sucesso");
            } else {
                $this->load->view("erro");
            }
        }

        public function listando_consultas() {
            $this->load->model("model_marcar_consulta");
            $consulta = $this->model_marcar_consulta->retorna_lista_consulta();
            $variaveis["consulta"] = $consulta;
            $this->load->view("listar", $variaveis);
        }
    }
?>

And this is my View:

<table border="1">
    <tr>
        <td>Id</td>
        <td>Especialidade</td>
        <td>Data de Marcação</td>
        <td>Horario de Marcação</td>
        <td>Observação</td>
        <td>Data de públicação</td>
    </tr>

    <?php foreach($consulta -> result() as $linha): ?>
        <tr>
            <td><?php echo $linha->id ?></td>
        </tr>

    <?php endforeach; ?>

</table>

I really don’t understand this mistake:

PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/listar.php

Line Number: 21

Fatal error: Call to a Member Function result() on a non-object in C: Program Files (x86) Easyphp-5.3.8.0 www marcar_query application views listar.php on line 21

  • 2

    You have already checked the return of the method $this->model_marcar_consulta->retorna_lista_consulta()?

  • Hey, valdiney, check it out editions of the question to see how to format a post here in the stack :)

  • If you post the code of your model it would be easier to help you. For without knowing what is encoded there we have to guide ourselves by deductions.

1 answer

1

Fatal error: Call to a Member Function result() on a non-object in C: Program Files (x86) Easyphp-5.3.8.0 www marcar_query application views listar.php on line 21

In his view you use $consulta-> result(), but the error informs that $consulta is not an object. It seems that your model is returning an array with the result of the operation - with $query->result(), and not the instacing of DB.

1) If your model is running the query with $this->db->get()->result(), its loop must reference the object:

<?php foreach($consulta as $linha): ?>
   <tr>
       <td><?php echo $linha->id ?></td>
   </tr>
<?php endforeach; ?>

2) To use the result in array form, use $this->db->get()->result_array()

<?php foreach($consulta as $linha): ?>
   <tr>
       <td><?php echo $linha['id'] ?></td>
   </tr>
<?php endforeach; ?>

More examples in DOC of Codeigniter on results of DB operations.

Browser other questions tagged

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