我想要完成的任务:我正试图在用户之间显示通信消息。
到目前为止我有什么:
控制器:
public function view()
{
$thread_id = $this->uri->segment(4);
$this->data['reply'] = $this->enquire_m->get_messages($thread_id);
}
型号:
public function get_messages($thread_id='')
{
$this->load->model('user_m');
$this->db->trans_start();
$query_str = $this->db->query('SELECT *
FROM message_replies
WHERE message_replies.thread_id = $thread_id');
$result = $this->db->query($query_str);
return $result;
$this->db->trans_complete();
}
查看:
<ul>
<?php
$received_message = $this->enquire_m->get_messages();
foreach ($received_message->result() as $reply)
{
'<li>
<div class="message-bubble">
<span class="bubble-user">'.$reply->name_surname.'</span>
<div class="bubble_left palette palette-clouds" data-domelement="" style="display: block;">
'.$reply->message.'
<div class="bubble_arrow"></div>
</div>
<span class="bubble-date">'.$reply->date;
}
?>
</ul>
DB:
thread_id int(11) NOT NULL,
id int(11) NOT NULL AUTO_INCREMENT,
sender_id int(11) NOT NULL,
recepient_id int(11) NOT NULL,
name_surname varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
date datetime DEFAULT NULL,
message text COLLATE utf8_unicode_ci,
readed binary(1) DEFAULT '0',
网址
index.php/admin/enquire/view/19
问题:没有显示任何内容。我在View上收到错误,它在
中给出了致命的错误foreach ($received_message->result() as $reply)
不幸的是,我看不出有什么问题,我希望有人能给我一个解决方案。
感谢您的时间!
答案 0 :(得分:1)
三个问题。
你必须记住控制器方法的工作是中间人。它将从模型中获取数据并将其发送到您的视图。然后,您的视图可以根据需要呈现该数据
在您的情况下,您的控制器需要传递这样的数据:
<强>控制器:强>
public function view() {
$thread_id = $this->uri->segment(4);
$this->data['replies'] = $this->enquire_m->get_messages($thread_id);
$this->load->view('enquire/view', $this->data);
}
然后在查看:
中<ul>
<?php
//$replies is the variable sent by the controller
foreach ($replies as $reply) {
echo '<li>
<div class="message-bubble">
<span class="bubble-user">'.$reply->name_surname.'</span>
<div class="bubble_left palette palette-clouds" data-domelement="" style="display: block;">
'.$reply->message.'
<div class="bubble_arrow"></div>
</div>
<span class="bubble-date">'.$reply->date;
}
?>
</ul>
我会在我的视图中以这种方式输出HTML:
<ul>
<?php foreach ($replies as $reply) { ?>
<li>
<div class="message-bubble">
<span class="bubble-user"><?php echo $reply->name_surname ?></span>
<div class="bubble_left palette palette-clouds" data-domelement="" style="display: block;">
<?php echo $reply->message ?>
<div class="bubble_arrow"></div>
</div>
<span class="bubble-date"><?php echo $reply->date ?>
<?php } ?>
</ul>
此外,我注意到您的HTML没有正确关闭</div>
请确保您更正。