Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:26:56+00:00 2026-06-14T09:26:56+00:00

I am learning Codeigniter 2 and successfully playing around with their Tutorial/Example about how

  • 0

I am learning Codeigniter 2 and successfully playing around with their Tutorial/Example about how to create a basic news application.

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.

I added this to my models file, news_model.php:

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

I added this to my controllers file, news.php:

public function delete($id) {
    $this->news_model->delete_news($id);

    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News item deleted';

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

I added this to my views file, index.php:

<a href="/news/delete/<?php echo $news_item['id'] ?>">Delete Item</a>

And finally, I added this to my config file, routes.php

$route['news/delete/(:any)'] = 'news/delete/$1';

It appears to be working as written.

From the News listing page at /news/, I click on the link for the news item and when the page reloads, the corresponding item is gone.

Questions:

1) After the page reloads, the URL is showing /news/delete/id, where /id is the item number. But, I don’t want this new URL, I really just want the /news/ 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?

2) In my Controller delete() function, after the first line, I am basically just repeating the same 5 lines of code from the Controller index() function. No doubt this is somehow related to question #1, but again, what is the better way to do this?

3) Anything else I missed relating to “standard” or “best practice”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:26:57+00:00Added an answer on June 14, 2026 at 9:26 am

    Thank-you to Jean-François G. B. and Stephan S. for pointing me in the right direction.

    This is the working solution…

    controllers file, news.php:

    public function delete($id) {
        $this->news_model->delete_news($id);
        $this->load->helper('url');
        redirect('/news/');
    }
    

    EDIT:

    Modified to display a confirmation message, “record #x deleted”…

    public function delete($id) {
        $this->news_model->delete_news($id);
        $this->load->helper('url');
        $this->load->library('session');
        $this->session->set_flashdata('error_message', 'record #'. $id . ' deleted');
        redirect('/news/');
    }
    

    and inside views file, index.php:

    <div id="message">
        <?php 
            if ( $this->session->flashdata('error_message') ) {                 
                echo $this->session->flashdata('error_message');
            }
        ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning CodeIgniter, and I'm following this authentication tutorial by nettuts+. I
I've just started learning about HMVC in CodeIgniter. So far I've been enjoying having
I'm just getting into learning about sessions, and for my purposes, I want to
I've been using the CodeIgniter framework, however after learning Java and awesome features like
I just learning yii framework and read this tutorial about yii how to setup
I recently started learning the CodeIgniter framework and i was wondering about one thing:
I have started playing with CodeIgniter now. And I use their user guide and
I'm learning CodeIgniter. I have a directory img with images (path /img/ ). I
I came from PHP language(codeigniter), but now I learning ASP.Net MVC :) In PHP
Learning about notifyAll made me question something about notify: in a typical situation we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.