2

好的,我的 Codeigniter 控制器中有这个:

public function post($id) {
$data['query'] = $this->blog_model->get_post($id);
$data['comments'] = $this->blog_model->get_post_comment($id);
$data['post_id']=$id;
$data['total_comments'] = $this->blog_model->total_comments($id);
$this->load->helper('form');
$this->load->library(array('form_validation','session'));
//validation rules for post function

$this->form_validation->set_rules('commenter','Name','required');
$this->form_validation->set_rules('email','Your email','required'|'valid_email');
$this->form_validation->set_rules('comment','Comment','required');

if($this->blog_model->get_post($id))
{
foreach($this->blog_model->get_post($id) as $row)
{
//set page title
$data['title'] = $row->entry_name;
}
if($this->form_validation->run() == FALSE)
{
//if validation runs FALSE
$this->load->view('blog/post',$data);
}
else
{
//if valid
        $name = $this->input->post('comment_name');
        $email = strtolower($this->input->post('comment_email'));
        $comment = $this->input->post('comment_body');
        $post_id = $this->input->post('entry_id');

        $this->blog_model->add_new_comment($post_id,$name,$email,$comment);
        $this->session->set_flashdata('message', '1 new comment added!');
        redirect('post/'.$id);
 }
 }
 else
 show_404();
 }

在我的帖子视图中,我有这个:

echo form_open('blog/post'.$id);

我想将$id我在 URL 中输入的变量传递给评论表单的帖子视图。如何将$id变量传递给视图?

4

2 回答 2

3

您在数据数组中传递id ,这里:

$data['post_id']=$id;

在您看来,而$id不仅仅是使用$post_id

echo form_open('blog/post'.$post_id);
于 2012-08-25T20:00:20.400 回答
1
$this->load->view('your post view',array('id'=>$the_id_here))

然后在您的视图中,使用 $id 变量。

于 2012-08-25T17:43:27.843 回答