Codeigniter – Update

Codeigniter – Update

This tutorial will show you how to edit record and save changes back to database using Codeigniter.

If you are new here, you may want to see

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 edit.

edit function is located after add function from CodeIgniter – Insert tutorial.

Our edit function will look like this

Site.php

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

        $data['component'] = $this->Component->get_row($componentId);

        if($post = $this->input->post())
        {
            $new = array(
                'ComponentID' => $componentId,
                'ComponentName' => $this->input->post('name'),
                'ComponentNo' => $this->input->post('quantity')
            );

            $this->Component->update($new);
            redirect(base_url());
        } else {
            $this->load->view('edit_component', $data);
        }
    }

 

Additional Settings

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

At the end of the file add this line

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

This will make our link to edit component look like

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

Instead of

http://localhost/codeigniter/site/edit/(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 / edit / (id)

 

Model

In Component.php Model we need two more functions.

First, we need function that will get a row from database for desired ComponentID.

It’s purpose is to display specific component on our edit_component.php view.

I’ll call it get_row and it looks like this

function get_row($id)
    {
        $this->db->where('ComponentID', $id);

        $query = $this->db->get('components');

        return $query->result();
    }

Create another function and call it update.

It will be used to update row in database with new data from edit form.

update function code is bellow

function update($data)
    {
        $this->db->where('ComponentID', $data['ComponentID']);
        $this->db->update('components', $data);
    }

 

View

On our homepage we will add a link to our controller’s edit function.

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></td>
                        </tr>
                    <?php endforeach; ?>

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

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

Next, we need a new view for editing component.

It is basically the same as view for adding component but has results from database in form fields.

edit_component.php

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

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

    <div class="container">
        <div class="page-header">
            <h1>Edit Hardware Component</h1>
        </div>

        <?php foreach($component as $row): ?>
        <div class="col-md-10">
            <?php if(validation_errors() != '') echo '<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><strong>' . validation_errors() . '</strong></div>'; ?>

            <?php echo form_open('edit/' . $row->ComponentID); ?>

            <div class="input-group input-group-lg">
                <input type="text" class="form-control" placeholder="Component Name" id="name" name="name" aria-describedby="sizing-addon1" value="<?php echo $row->ComponentName; ?>">
            </div>

            <br />

            <div class="input-group input-group-lg">
                <input type="text" class="form-control" placeholder="Component Quantity" id="quantity" name="quantity" aria-describedby="sizing-addon1" value="<?php echo $row->ComponentNo; ?>">
            </div>

            <br />

            <div>
                <input class="btn btn-primary btn-lg" href="#" role="button" type="submit" name="submit" value="Update" />
            </div>

            <?php echo form_close(); ?>
        </div>
        <?php endforeach; ?>
    </div>

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

 

Results

When we open our homepage, we’ll see Edit option in every row.

codeigniter-update-1

Click on Edit and Edit Component page will open, type another value and click on Update

codeigniter-update-2

After editing component you will be returned to homepage where you can see your edited component

codeigniter-update-3


Thank you for staying with us for yet another lesson of our Linux Web Zone CodeIgniter Series

Stay tuned, soon you’ll see how to delete record from database.

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

CodeIgniter Lesson 3 – Update

Leave a Reply

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