CodeIgniter View

1

I'm not able to display data in View .

Model bank

class ModelBanco extends CI_Model
{
    public $em;
    public $senha;
    public function __construct(){
        parent::__construct();
    }
    function get_user(){
        $this->db->select('*');
        $this->db->from('tb_user');
        $this->db->where('email','magno');
        return $this->db->get()->result();
    }
} 

WelcomeController

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

class Welcome extends CI_Controller {
   public function index()  { 
        $this->load->model('ModelBanco','',TRUE);
        $data['query']= $this->ModelBanco->get_user();
        $this->load->view('welcome_message',$data);
   }
}

Welcome_message View

<html>    
 <head>
 </head>
 <body>    
 </body>    
</html>

Database has been configured in the / config / database archiver

host='localhost';
username='root';
password='';
database='bd_imobiliaria';

The table inside the bank is tb_user and field email varchar(255) , there is a record with the data "magno". I would like to display the records on the screen with codeigniter in MVC just for testing. I added in my View the suggested code snippet.

echo $query->email;     

But this error is appearing.

  

@rray Severity: Notice

     

Message: Trying to get property of non-object

     

Filename: views / welcome_message.php

     

Line Number: 69

     

Backtrace:

     

File:   C: \ xampp \ htdocs \ ci_imobiliaria \ application \ views \ welcome_message.php   Line: 69 Function: _error_handler

     

File:   C: \ xampp \ htdocs \ ci_imobiliaria \ application \ controllers \ Welcome.php   Line: 27 Function: view

     

File: C: \ xampp \ htdocs \ ci_imobiliaria \ index.php Line: 292 Function:   require_once

I've done some research on non-object .

    
asked by anonymous 10.12.2015 / 18:00

2 answers

2

To correctly display the record in view , be aware of the name given to $data because the name of that key will change to a variable.

$data['query']= $this->ModelBanco->get_user();

In view call:

<?php echo $query[0]->email; ?>

Or change the model to return only one line with row ()

function get_user(){
    $this->db->select('*');
    $this->db->from('tb_user');
    $this->db->where('email','magno');
    return $this->db->get()->row();
}

In view :

<?php echo $query->email; ?>
    
10.12.2015 / 18:54
1

Thanks to everyone who helped me, I'll post the result.

Model

function get_user()
{  
        $this->db->select ('*')->from('tb_user');  
        $sql= $this->db->get();  
        $post =array();  
        foreach ($sql->result() as $row){  
            $post[$row->id_user] = $row->email;  
        }
}  

Controller

public function index() 
{  
    $this->load->model('modelbanco','',TRUE);  
    $data['post']= $this->modelbanco->get_user();  
    $this->load->view('welcome_message.php',$data);  
}  

View

<?php foreach ($post as $id_user =>$email) : ? >   
<?php echo $email; ? > (ID: < ?php echo $id_user; ? >)   
<?php endforeach; ? >
    
16.12.2015 / 04:20