0

我正在使用 CodeIgniter 并完成了新闻教程。我正在尝试在新闻部分添加删除和更新按钮。我已经在 stackoverflow 上看到有关本教程中删除按钮的帖子,但我仍然无法使其正常工作。

这是我从您的帖子中使用的编码

news_model.php

<?php
class News_model extends CI_Model {

public function __construct()
{
    $this->load->database();
}

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();
}

public function set_news()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
    'title' => $this->input->post('title'),
    'slug' => $slug,
    'text' => $this->input->post('text')
);

return $this->db->insert('news', $data);
 }


public function delete_news($id) {
$this->db->delete('news', array('id' => $id));
}

}





 CONTROLLER - news.php

   <?php
   class News extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('news_model');
}

public function index()
{
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');

}

public function view($slug)
{
    $data['news'] = $this->news_model->get_news($slug);
    if (empty($data['news_item']))
{
    show_404();
}

$data['title'] = $data['news_item']['title'];

$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');

}

public function create()
   {
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
    $this->load->view('templates/header', $data);   
    $this->load->view('news/create');
    $this->load->view('templates/footer');

}
else
{
    $this->news_model->set_news();
    $this->load->view('news/success');
}
 }


   public function delete($id) {
   $this->news_model->delete_news($id);
   $this->load->helper('url');
   redirect('/www.shelim786.co.uk/CodeIgniter/news');
}




}




VIEWS - index.php 

<?php foreach ($news as $news_item): ?>

<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
    <?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<p><a href="news/delete <?php echo $news_item['slug'] ?>">delete article</a></p>

<?php endforeach ?>

and ROUTING

  $route['news/create'] = 'news/create';
  $route['news/(:any)'] = 'news/view/$1';
  $route['news'] = 'news';
  $route['(:any)'] = 'pages/view/$1';
  $route['default_controller'] = 'pages/view';
  $route['news/delete/(:any)'] = 'news/delete/$1';

我遇到的问题是,当我单击删除按钮时,www.shelim786.co.uk/CodeIgniter/index.php/news 中的新闻文章没有被删除。

如果您能帮助我,我将不胜感激。

谢谢,

谢利姆

4

2 回答 2

0

你应该重定向你的删除链接delete_news 函数
而不是删除函数,因为删除查询写在那里

于 2013-11-26T17:21:16.450 回答
0

链接中的“删除”一词后没有斜线。您的链接会显示为http://www.shelim786.co.uk/CodeIgniter/index.php/news/delete 6. 注意“删除”后的空格?

在您看来,将“删除文章”链接行更改为此。

<p><a href="news/delete/<?php echo $news_item['slug'] ?>">delete article</a></p>

注意到“删除”后的斜线了吗?

于 2013-03-09T14:27:24.493 回答