Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter News Tutorial - Delete and Update
    text
    copied!<p>I am new to CodeIgniter, and I have followed the tutorial to make a news application. I am now on deleting and updating the news article but I can't figure out how to do it. </p> <p>This is what I have come up with so far, but it doesn't work because when I click delete article in index all that happens is that I get an error message saying : <code>webpage cannot be found</code>.</p> <p>news controller :</p> <pre><code>&lt;?php class News extends CI_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;model('news_model'); } public function index() { $data['news'] = $this-&gt;news_model-&gt;get_news(); $data['title'] = 'News archive'; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/index', $data); $this-&gt;load-&gt;view('templates/footer'); } public function view($slug) { $data['news'] = $this-&gt;news_model-&gt;get_news($slug); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/view', $data); $this-&gt;load-&gt;view('templates/footer'); } public function create() { $this-&gt;load-&gt;helper('form'); $this-&gt;load-&gt;library('form_validation'); $data['title'] = 'Create a news item'; $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required'); $this-&gt;form_validation-&gt;set_rules('text', 'text', 'required'); if ($this-&gt;form_validation-&gt;run() === FALSE) { $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/create'); $this-&gt;load-&gt;view('templates/footer'); } else { $this-&gt;news_model-&gt;set_news(); $this-&gt;load-&gt;view('news/success'); } } public function delete($id) { $this-&gt;news_model-&gt;delete_news($id); $this-&gt;load-&gt;helper('url'); redirect('/news'); } } </code></pre> <p>news_model:</p> <pre><code>&lt;?php class News_model extends CI_Model { public function __construct() { $this-&gt;load-&gt;database(); } public function get_news($slug = FALSE) { if ($slug === FALSE) { $query = $this-&gt;db-&gt;get('news'); return $query-&gt;result_array(); } $query = $this-&gt;db-&gt;get_where('news', array('slug' =&gt; $slug)); return $query-&gt;row_array(); } public function set_news() { $this-&gt;load-&gt;helper('url'); $slug = url_title($this-&gt;input-&gt;post('title'), 'dash', TRUE); $data = array( 'title' =&gt; $this-&gt;input-&gt;post('title'), 'slug' =&gt; $slug, 'text' =&gt; $this-&gt;input-&gt;post('text') ); return $this-&gt;db-&gt;insert('news', $data); } public function delete_news($id) { $this-&gt;db-&gt;delete('news', array('id' =&gt; $id)); } } </code></pre> <p>index :</p> <pre><code>&lt;?php foreach ($news as $news_item): ?&gt; &lt;h2&gt;&lt;?php echo $news_item['title'] ?&gt;&lt;/h2&gt; &lt;div id="main"&gt; &lt;?php echo $news_item['text'] ?&gt; &lt;/div&gt; &lt;p&gt;&lt;a href="news/&lt;?php echo $news_item['slug'] ?&gt;"&gt;View article&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="news/delete/&lt;?php echo $news_item['id'] ?&gt;"&gt;delete article&lt;/a&gt;&lt;/p&gt; &lt;?php endforeach ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload