Note that there are some explanatory texts on larger screens.

plurals
  1. POModifying standard Codeigniter 2 news tutorial with a delete function
    primarykey
    data
    text
    <p>I am learning Codeigniter 2 and successfully playing around with <a href="http://codeigniter.com/user_guide/tutorial/index.html" rel="nofollow">their Tutorial/Example about how to create a basic news application</a>.</p> <p>So after getting this working, I wanted to try to add a "delete record" link to the news item to see if I could figure this out on my own.</p> <p>I added this to my <strong>models</strong> file, <code>news_model.php</code>:</p> <pre><code>public function delete_news($id) { $this-&gt;db-&gt;delete('news', array('id' =&gt; $id)); } </code></pre> <p>I added this to my <strong>controllers</strong> file, <code>news.php</code>:</p> <pre><code>public function delete($id) { $this-&gt;news_model-&gt;delete_news($id); $data['news'] = $this-&gt;news_model-&gt;get_news(); $data['title'] = 'News item deleted'; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/index', $data); $this-&gt;load-&gt;view('templates/footer'); } </code></pre> <p>I added this to my <strong>views</strong> file, <code>index.php</code>:</p> <pre><code>&lt;a href="/news/delete/&lt;?php echo $news_item['id'] ?&gt;"&gt;Delete Item&lt;/a&gt; </code></pre> <p>And finally, I added this to my <strong>config</strong> file, <code>routes.php</code></p> <pre><code>$route['news/delete/(:any)'] = 'news/delete/$1'; </code></pre> <p>It appears to be working as written.</p> <p>From the News listing page at <code>/news/</code>, I click on the link for the news item and when the page reloads, the corresponding item is gone.</p> <p>Questions:</p> <p>1) After the page reloads, the URL is showing <code>/news/delete/id</code>, where <code>/id</code> is the item number. But, I don't want this new URL, I really just want the <code>/news/</code> page to reload to show the new content. Obviously, the way I did this is potentially dangerous, because a simple page refresh by the user would delete another item. What is the standard way to fix/handle this?</p> <p>2) In my Controller <code>delete()</code> function, after the first line, I am basically just repeating the same 5 lines of code from the Controller <code>index()</code> function. No doubt this is somehow related to question #1, but again, what is the better way to do this?</p> <p>3) Anything else I missed relating to "standard" or "best practice"?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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