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
.
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.
– Magno Malkut
@Magnomalkut on index, do:
print_r($this->ModelBanco->get_user());
– rray
The connection to the bank is working. result:"Array ( [0] => Array ( [id_user] => 1 [email] => magno [password] => 12345 [user_session_id] => [user_permissions] => ) )"
– Magno Malkut
@Magnomalkut, edit the answer now.
– rray
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?
– Magno Malkut
@Magnomalkut, the
row()
returns only one line, theresult()
returns more than one, so you need the foreach.– rray
I researched some examples, and saw that it is quite common for the view to communicate with the model directly, by MVC is correct?
– Magno Malkut
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
– rray
Okay, I get the explanation. Thank you so much for all your help.
– Magno Malkut