2013-03-09 63 views
0

我是CodeIgniter的新手,我已經按照教程創建了一個新聞應用程序。我現在正在刪除和更新新聞文章,但我無法弄清楚如何去做。CodeIgniter新聞教程 - 刪除和更新

這是我到目前爲止,但它不起作用,因爲當我點擊刪除索引中的文章發生的所有事情是,我收到一條錯誤消息說:webpage cannot be found

消息控制器:

<?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('/news'); 
    } 

} 

news_model:

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

} 

指數:

<?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['id'] ?>">delete article</a></p> 

<?php endforeach ?> 

回答

0

,你必須使用絕對路徑

<a href="<?php echo base_url(); ?>news/delete/<?php echo $news_item['id'] ?>">delete article</a> 

也是在後刪除

,而不是

redirect('/news'); 

使用absulot路徑或控制器,你可以簡單地寫

$this->index();