我有一个消息列表,当点击标题时,它会将它们带到另一个视图中,在那里他们可以看到扩展的消息。
这是我点击链接的视图。
帖子的观点。
<a href="<?=base_url();?>index.php?/Message/display<?=$row['id']?>">Link</a>
Message Controller:
class Message extends CI_Controller {
var $TPL;
public function __construct()
{
parent::__construct();
}
private function display()
{
$query = $this->db->query("SELECT FROM messages WHERE id = '$id';");
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
public function index()
{
$this->display();
}
}
<<p> 消息视图/strong> <?$int=0;?>
<? foreach ($threads as $row) { ?>
<div class="row">
<div class="message">
<h3><?= $row['title']?></h3>
<p><?= $row['message']?></p>
<p><?= $row['member']?></p>
</div>
</div>
<hr>
<? $int++;?>
<? } ?>
这很简单。修改代码如下
<a href="<?php echo site_url('message/display').'/'.$row['id'];?>">Link</a>
然后将display
方法更改为public并在那里发送ID参数,如
public function display($id){
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
最后去掉$int=0
&$int++;
从视图文件,因为你不使用这个。现在测试
更改链接
<a href="<?php echo site_url().'message/display/'.$row['id'];?>">Link</a>
在控制器
public function display() {
$id=$this->uri->segment(3);
if($id==null) {
redirect('Index');
}
else {
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
}