我有一个小问题,我正在尝试为用户显示消息,该消息应该采用我选择的用户的参数。
但我不确定这里出了什么问题
控制器
<?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);
}
}
模型
<?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();
}
}
查看
<!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>
编辑:错误 遇到PHP错误
严重性:注意
消息:未定义属性:User :: $ Messages_model
文件名:controllers / User.php
行号:13
答案 0 :(得分:2)
检查一下
控制器:(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);
}
}