0

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

2
  • Where do you see the undefined property ? In the foreach in your view ? Could you paste the exact error ? Commented Nov 18, 2014 at 8:32
  • sorry, i edited my post Commented Nov 18, 2014 at 8:34

1 Answer 1

2

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);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Your model page was Messages_model.php but you are loading Messages_Model.php . it just a Case sensitive issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.