【发布时间】:2014-09-10 15:59:29
【问题描述】:
我完成了在 codeigniter user_guide 上制作新闻应用程序的教程。我现在正在尝试扩展教程网站,以便它将文本加载到可以重新输入并用于更新条目的文本区域中。我希望查看文章页面是用于更新的同一页面。视图加载正确,但是当我单击提交时,它会将我从 localhost/ci/news/foo 和 404 带到 localhost/ci/news/view。我主要尝试模仿创建,只更改模型查询,但由于我一直在苦苦挣扎,因此尝试了几种想法。
我怀疑问题可能是以下之一。
某事将我引导至 ci/news/view 滥用我的 $slug 变量 控制器和模型连接错误 我的模型查询中的语法。
我会发布所有相关代码。
这是我的视图控制器函数,这也是发生更新的地方。
public function view($slug)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['news_item'] = $this->news_model->get_news($slug);
$slug = $slug;
$this->form_validation->set_rules('text', 'text', 'required');
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news();
$this->load->view('news/success');
}
}
这是我在模型中的 update_news()
public function update_news($slug)
{
$data = array(
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->set('slug', $slug);
$this->db->update('news', $data);
}
还有我的view.php
<?php
echo validation_errors();
echo form_open('news/view');
echo '<h2>'.$news_item['title'].'</h2>';
?>
<label for="text">Text</label>
<textarea name="text"><?php echo $news_item['text']; ?></textarea><br />
<input type="submit" name="submit" value="Update" />
</form>
更新:根据要求,我的 get_news 模型函数。
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
【问题讨论】:
-
你能添加你的“get_news”功能吗?
标签: php mysql codeigniter