Codeigniter View

Asked

Viewed 371 times

1

I am not able to present data on 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 configured in archive /config/database

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 present 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.

2 answers

2

To correctly display the record on view, be attentive to the name given in $data because the name of this key will become a variable.

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

In the view calling:

<?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 the view:

<?php echo $query->email; ?>
  • I ran the suggestion but still an error appears in the view, the "error" is in the code itself above. I searched on "Trying to get Property of non-object", I understood that my get property of the model class is not an object, I even tested a foreach to go through the 'query' but also without result.

  • @Magnomalkut on index, do: print_r($this->ModelBanco->get_user());

  • The connection to the bank is working. result:"Array ( [0] => Array ( [id_user] => 1 [email] => magno [password] => 12345 [user_session_id] => [user_permissions] => ) )"

  • @Magnomalkut, edit the answer now.

  • Based on your suggestion, I did something that worked: $query= $this->db->get('tb_user'); foreach ($query->result() as $Row){ echo $Row->email; }, but my question is, this way I’m not using the controller?

  • 1

    @Magnomalkut, the row() returns only one line, the result() returns more than one, so you need the foreach.

  • I researched some examples, and saw that it is quite common for the view to communicate with the model directly, by MVC is correct?

  • 1

    The view usually receives the model, but the view does not communicate with the model directly, the controller does this communication between the two. @Magnomalkut

  • Okay, I get the explanation. Thank you so much for all your help.

Show 4 more comments

1

I thank everyone who helped me, I will 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; ? >

Browser other questions tagged

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