使用form_open函数将get参数添加到URL吗?

时间:2018-07-31 08:05:29

标签: php codeigniter codeigniter-3

我尝试实现用于添加,编辑文章到博客的标准PHP逻辑,所以我有 add方法

public function add()
{
    $this->load->model('admin/Blog_Model');

    if (!empty($this->input->post())) {
        $user = $this->Blog_Model->addPost($this->input->post());
    }

    $this->getForm();
}

编辑方法:

public function edit($id = '')
{
    $this->load->model('admin/Blog_Model');

    if (!empty($this->input->post())) {
        var_dump($id); exit;
        $user = $this->Blog_Model->editPost($this->input->post(), $id);
    }

    $this->getForm($id);

}

getForm方法:

public function getForm($id = '')
{
    if (!empty($id)) {
        $post = $this->Blog_Model->getPost($id);
        $data['action'] = 'admin/blog/edit';
    } else {
        $data['action'] = 'admin/blog/add';
    }

    $data['formTitle'] = array(
        'name'          => 'title',
        'id'            => 'content-title',
        'value'         => isset($post['title']) ? $post['title'] : '',
        'placeholder'   => 'Заглавие',
        'class'         => 'form-control'
    );

    $data['formContent'] = array(
        'name'          => 'content',
        'id'            => 'content-blog',
        'value'         => isset($post['content']) ? $post['content'] : '',
        'placeholder'   => 'Съдържание',
    );

    $data['formButton'] = array(
        'type' => 'submit',
        'content'=> 'Изпрати',
        'class'=> 'btn btn-primary btn-block btn-flat'
    );

    $data['head'] = $this->load->view('admin/head', NULL, TRUE);
    $data['left_column'] = $this->load->view('admin/left_column', NULL, TRUE);

    $this->load->view('admin/header', $data);
    $this->load->view('admin/blog_form', $data);
    $this->load->view('admin/footer');

}

blog_form视图,格式为:

    <div class="box-body pad">
        <?php echo form_open($action); ?>

        <div class="box-body">
        <div class="form-group">
          <label for="content-title">Заглавие:</label>
           <?php echo form_input($formTitle); ?>
        </div>
        <div class="form-group">
            <label for="content_blog">Съдържание:</label>
            <?php echo form_textarea($formContent); ?>
        </div>
        <div class="col-xs-12 col-md-3 pull-right">
            <?php echo form_button($formButton); ?>
        </div>
      </div>
      <?php echo form_close(); ?>
    </div>

所以..一切正常,但是我对此部分有疑问:

    if (!empty($id)) {
        $post = $this->Blog_Model->getPost($id);
        $data['action'] = 'admin/blog/edit';
    } else {
        $data['action'] = 'admin/blog/add';
    }

如果是编辑,我想发送 id ,例如GET参数。我认为问题出在当我在此处显示所有文章时,我没有使用标准的GET参数而不是site_url函数:

            <?php foreach ($posts as $post) { ?>
            <tr>
              <td><?php echo $post['id']; ?></td>
              <td><?php echo $post['title']; ?></td>
              <td><?php echo $post['content']; ?></td>
              <td><div class="btn-group">
                  <a href="<?php echo site_url('admin/blog/edit/'.$post['id']); ?>" class="btn btn-success">Редактирай</a>
                  <a href="<?php echo site_url('admin/blog/delete/'.$post['id']); ?>" class="btn btn-danger">Изтрий</a>
                </div></td>
            </tr>
            <?php } ?>

1 个答案:

答案 0 :(得分:1)

尝试更改

if (!empty($id)) {
    $post = $this->Blog_Model->getPost($id);
    $data['action'] = 'admin/blog/edit';
} else {
    $data['action'] = 'admin/blog/add';
}

if (!empty($id)) {
    $post = $this->Blog_Model->getPost($id);
    $data['action'] = 'admin/blog/edit/'.$id; // pass $id to edit controller
} else {
    $data['action'] = 'admin/blog/add';
}