CodeIgniter – Delete

Codeigniter – Delete

This tutorial will show you how to delete record from database using Codeigniter.

If you are new here and did not see previous tutorials, you may want to visit

Linux Web Zone CodeIgniter Series

There you can find links for all lessons from our CodeIgniter Series


Controller

We will edit Site.php controller and add new function called delete.

delete function is located after edit function from CodeIgniter – Update tutorial.

Our delete function will look like this

Site.php

public function delete()
    {
        $componentId = $this->uri->segment(2);

        $this->Component->delete($componentId);

        redirect(base_url());
    }

 

Additional Settings

Open routes.php file located in application/config directory.

At the end of the file add this line

$route['delete/(:num)'] = "site/delete/$1";

This will make our link to edit component look like

http://localhost/codeigniter/delete/(id)

Instead of

http://localhost/codeigniter/site/delete/(id)

This is optional and if you decide to skip this step, you need to edit Site.php Controller and change this line

        $componentId = $this->uri->segment(2);

To this

        $componentId = $this->uri->segment(3);

This will take the third segment of URL – site / delete / (id)

 

Model

In Component.php Model we need a function that will delete record from database.

It will remove from database component with given ComponentID.

I’ll call it delete and it looks like this

Component.php

function delete($id)
    {
        $this->db->where('ComponentID', $id);
        $this->db->delete('components');
    }

 

View

We’ll only add option for deleting component in table.

There is no need for an additional views because everything goes through controller and model so we only need URL to call controllers function for deleting record.

site.php

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<?php $this->load->view('templates/header'); ?>

    <!-- Begin page content -->
    <div class="container">
        <div class="page-header">
            <h1>Hardware Components</h1>
        </div>

        <div class="col-md-10">
            <div class="table-responsive">
                <table class="table table-condensed table-hover">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>Component Name</th>
                        <th>Quantity</th>
                        <th>Options</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php if(empty($components)) echo '<tr><td colspan="4"><em>There are no added components!</em></td></tr>'; ?>

                    <?php foreach ($components as $row): ?>
                        <tr>
                            <td><?php echo $row->ComponentID; ?></td>
                            <td><?php echo $row->ComponentName; ?></td>
                            <td><?php echo $row->ComponentNo; ?></td>
                            <td><a href="<?php echo base_url() . 'edit/' . $row->ComponentID; ?>">Edit</a> | <a href="<?php echo base_url() . 'delete/' . $row->ComponentID; ?>">Delete</a></td>
                        </tr>
                    <?php endforeach; ?>

                    </tbody>
                </table>
            </div>
        </div>
    </div>

<?php $this->load->view('templates/footer'); ?>

Results

Our homepage will look like this now

codeigniter-delete-1

After we click Delete link, record will be deleted from database and we’ll be returned to homepage where there is one component less

codeigniter-delete-2


This completes Lesson 4 of our Linux Web Zone CodeIgniter Series

Stay tuned because we are only warming up for things to come.

 

You can download complete source code from this tutorial on the link bellow

CodeIgniter Lesson 4 – Delete

Leave a Reply

Your email address will not be published. Required fields are marked *