Php CodeIgniter新闻教程删除
我正在使用CodeIgniter,已经完成了新闻教程。我试图在新闻部分添加一个删除和更新按钮。我已经在stackoverflow上看到了你关于本教程中删除按钮的帖子,但我仍然无法让它工作 这是我在你的帖子中使用的代码 news_model.phpPhp CodeIgniter新闻教程删除,php,codeigniter,Php,Codeigniter,我正在使用CodeIgniter,已经完成了新闻教程。我试图在新闻部分添加一个删除和更新按钮。我已经在stackoverflow上看到了你关于本教程中删除按钮的帖子,但我仍然无法让它工作 这是我在你的帖子中使用的代码 news_model.php <?php class News_model extends CI_Model { public function __construct() { $this->load->database(); } public fun
<?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';
和路由
$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上的新闻文章
单击“删除”按钮时不删除
如果你能帮助我,我将不胜感激
谢谢
Shelim链接中“删除”一词后没有斜杠。您的链接会显示类似
http://www.shelim786.co.uk/CodeIgniter/index.php/news/delete 6
。注意“delete”后面的空格吗
在您看来,将“删除文章”链接行更改为此
<p><a href="news/delete/<?php echo $news_item['slug'] ?>">delete article</a></p>
请注意“delete”后的斜杠?您应该重定向删除链接删除新闻功能
不删除功能因为删除查询写在那里我已经将删除文章链接更改为,它出现了一个404错误,文章仍然不删除。有什么我做错了吗?控制器的“新闻”一词在链接中重复出现。这是因为在分配删除链接时使用的是相对路径。请尝试删除链接<代码>。我假设您已打开url帮助程序。事实上,我建议您使用
site\u url()
来分配所有链接,包括查看链接。我花了相当长的时间来找出分配链接的致命错误:调用未定义的函数site_url()在第8行的/customers/5/f/b/shelim786.co.uk/httpd.www/CodeIgniter/application/views/news/index.php中,这是我在将链接分配到时遇到的错误。我也是CodeIgniter的新手,这是我第一次使用它,所以我不确定如何打开TinyUrl。它们都在手册中。对于url帮助程序,请查看。或者,您可以打开应用程序/autoload.php
,每次都将“url”添加到自动加载url帮助程序的帮助程序数组中。我强烈建议您至少阅读一到两次整个CodeIgniter手册。他们拥有MVC框架中最好的手册之一