Reputation: 5
I have a small problem, i am trying to display messages for a user which should take a parameter of the user i choose.
But i am not sure whats going wrong here
Controller
<?php
class User extends CI_Controller {
public function index(){
$this->load->view('ViewMessages');
}
public function view($name)
{
$this->load->model("Messages_Model");
$data ['results'] = $this->Messages_model->getMessagesByPoster($name);
$this->load->view("ViewMessages", $data);
}
}
Model
<?php
class Messages_model extends CI_Model{
function getMessagesByPoster($name)
{
$this->load->database();
$query = $this->db->query("SELECT * FROM messages WHERE user_username='$name'");
return $query->result();
}
}
View
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>View Messages</h1>
<h2></h2>
<div id="Main">
<?php
foreach($results as $row){
echo $row ->id;
echo $row ->user_username;
echo $row ->text;
echo $row ->posted_at;
echo "<br/>";
}
?>
</div>
</body>
</html>
EDIT: Error A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$Messages_model
Filename: controllers/User.php
Line Number: 13
Upvotes: 0
Views: 4686
Reputation: 5596
Check this one
Controller: (User.php)
<?php
class User extends CI_Controller {
public function index(){
$this->load->view('ViewMessages');
}
public function view($name)
{
$this->load->model("Messages_model"); //Case sensitive you have written Messages_Model
$data ['results'] = $this->Messages_model->getMessagesByPoster($name);
$this->load->view("ViewMessages", $data);
}
}
Upvotes: 2