1

我正在使用 Codeigniter 框架发展我的技能。我已经查看了数据并将其插入到数据库中,发现更新数据更加棘手。我见过的大多数教程都是在代码中输入值,而不是从数据库中提取选定的 id 并在表单字段中回显。到目前为止,我有:

新闻模型:

function editArticle($data) {
        $data = array(
                       'title' => $title,
                       'content' => $content,
                       'author' => $author
                    );

        $this->db->where('id', $id);
        $this->db->update('news', $data, array('id' =>$id));

    }

控制器:

    public function update_entry() {
        //load the upate model
        $this->load->model('update_model');

        //get the article from the database
        $data['news'] = $this->news_model->get_article($this->uri->segment(4));

        // perform validation on the updated article so no errors or blank fields
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('content', 'Content', 'trim|required');
        $this->form_validation->set_rules('author', 'Author', 'trim|required');

        // If validation fails, return to the edit screen with error messages
        if($this->form_validation->run() == FALSE) {

            $this->index();

        }else{
            //update the news article in the database
            if($query = $this->update_model->update()) {

        }else{
            redirect('admin/edit');
        }
    }
}

看法:

        <?php echo form_open('admin/edit/edit_article'); ?>

        <?php echo form_input('title', set_value('title', 'Title')); ?><br />
        <?php echo form_textarea('content', set_value('content', 'Content')); ?><br />
        <?php echo form_input('author', set_value('author', 'Author')); ?>
        <?php echo form_submit('submit', 'Edit Article'); ?>
        <?php if (isset($error)){echo "<p class='error'>$error</div>";
        }?>
        <?php echo validation_errors('<p class="error">' );?>
         <?php echo form_close(); ?> 

1)我不确定如何回显数据(从用户从所示文章视图中单击编辑按钮时)以获取该 ID,然后在文本字段中的编辑页面上显示。

2)然后让用户提交更新的数据并发布到数据库中?

对于构建我的其余控制器/视图文件的任何指导或帮助,我将不胜感激,因为我已经在这方面工作了一天多!

谢谢你。

4

1 回答 1

0

你还没有给出你的观点的所有代码,所以我不确定你有多少是正确的,你有多少是错误的,但我会提到一些我能看到的东西 -

您似乎没有像$this->load->view('edit', $data);(请参阅http://codeigniter.com/user_guide/general/views.html)那样从当前字段的内容所在的控制器调用您的视图$data

要预填充表单字段,请将当前字段值放在第二个参数中,set_value()例如set_value('title', $article->title).

您的模型还需要处理提交的表单 (in $this->input->post),然后在模型中调用更新查询。

(我不得不说 CodeIgniter 文档在这方面不是很好 - 你必须查看Form HelperForm Validation Class文档,以及跟踪 Views (上面的链接)、ControllersModels(加上一两个其他人,我不应该怀疑))。

于 2012-09-06T15:04:59.393 回答