Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Web Development

Module 5 – Updating and Deleting data using CodeIgniter

Topics
Introduction .................................................................................................................................. 2

Update Record .............................................................................................................................. 4

Delete Record ............................................................................................................................... 7

Flashdata and Tempdata ............................................................................................................... 8

Flashdata ................................................................................................................................................ 8

Tempdata ............................................................................................................................................... 9

Learning Objectives

At the end of the module the student is expected to:

1. Understand and apply the concept of Updating and Deleting Records in CodeIgniter

2. Know the use of Flashdata and Tempdata

3. Apply the functionality of Flash and Temp data in Code igniter

Module 5 – Updating and Deleting data using CodeIgniter 1


Web Development

Introduction

Last chapter we discussed about Form Validation and Inserting Records to our Database. What if

we need to change or edit our records? Or there are records need to be cleared or deleted?

Let’s say we have this scenario; we have table of ‘flowers’ in our database.

flower_id flower_name price isAvaiable

1 Roses 20 1

2 Tulips 15 0

3 Lily 25 1

isAvailable tell us if it is Available or not. 1 means available and 0 means it’s not.

The Flower controller at application/controllers/Flower.php

$data[“flowers”] contains array of records using the getFlowers() method from the model at

application/models/flowers_model.php

Module 5 – Updating and Deleting data using CodeIgniter 2


Web Development

getFlowers() can be used to fetch a single or a bunch of records.

Flower Listings view at application/views/flowers.php

Module 5 – Updating and Deleting data using CodeIgniter 3


Web Development

A typical html structure with a table containing record loops of flowers. Inside the foreach loop

on the last td element, contains 2 action buttons: Edit and Delete. Inside their href attribute, there is a

link passing the flower_id to the controller using base_url(). The base_url function of the URL helper

returns the $config[‘base_url’] value defined on application/config/config.php. It appends the literal

string parameter to the base URL.

Output:

We now have a flower listings webpage with an action buttons on each row. Beyond this point

of this example scenario, we need to perform altering of the flower records. We’ll create a form to allow

editing any flower record and also a script to delete it.

Update Record

Add the code below on the controller for the Edit functionality:

The edit method will use the passed flower_id from the Flower Listings to edit the specific flower

record and load the view for editing.

Create the editing form view at application/views/flower_edit.php:

Module 5 – Updating and Deleting data using CodeIgniter 4


Web Development

The flower_edit.php view uses form helpers to output HTML form elements. The flower record

properties will be automatically loaded inside each form elements. We use extract() php function to

make variables out of the $flower array.

On line 21, we use the ternary if operator to return a true/false ‘checked’ value on the third

parameter of form_checkbox.

Upon form submission, it will be redirected to the verifyEdit method of the Flowers controller.

verifyEdit contains form validation to make sure the correct data will be inserted properly on our

database server.

We also use validation_errors() to present form errors if they exists.

Module 5 – Updating and Deleting data using CodeIgniter 5


Web Development

The verifyEdit method on Flowers controller:

Do you remember uri segments? We use $this->uri->segment(3) to get the flower_id from our

URI. Validations were made to require the fields and make the accepted input price decimal. If it passed

the validation and returned TRUE, it will call the update() method from our model to update the record

and tell whether it is updated or not using echo php function.

The update method on the flowers_model:

Query Builder class’ $this->db->update() was used to execute the update query. This method

will return a Boolean value TRUE if Successful and FALSE if it contains errors.

Module 5 – Updating and Deleting data using CodeIgniter 6


Web Development

Output:

Delete Record

Add the code below on the controller for the Delete functionality:

The delete method of the Flowers controller checks the flower_id first if that id exists on the

table before deletion. Upon execution of the delete method of the model, it will print out if its deleted

or not.

The delete method of the flowers_model:

This code will return a Boolean value of TRUE/FALSE of the delete query

Module 5 – Updating and Deleting data using CodeIgniter 7


Web Development

Output:

The figure on the left shows our flower listings. If a flower record’s delete button was clicked, a

javascript confirmation dialogue box will appear (see reference view for the code). When a user selects

OK, it will delete the item and show the screen:

Flashdata and Tempdata

Codeigniter has its own session class including flashdata and tempdata. These are session

variables that has its own set of constraints and used in a very quick instance. They are mostly efficient

in displaying notifications such as success and failed status messages.

Flashdata

Flashdata variables are just like session variables except it is only available on the next request.

It is called “flash” because you may pass and use values in an instant and gone in a flash.

Setting flashdata:

Module 5 – Updating and Deleting data using CodeIgniter 8


Web Development

set_flashdata is used to set the flashdata session variable and it require 2 parameters: the

variable name and its value.

Using flashdata:

You can access the variable you have set using $this->session->flashdata(‘variable_name’)

Tempdata

Tempdata variables are session variables that expires with a given time limit.

Setting tempdata:

set_tempdata is used to set temporary and timed session variables. Its parameters are the

variable name, the variable value, and the expiry time of the variable (in seconds).

Using tempdata:

You may use the tempdata using $this->session->tempdata(‘variable_name’)

Flashdata and Tempdata can set session variables out of an associative array by using

set_flashdata or set_tempdata by taking the array as the only parameter for the flashdata:

Same goes for the tempdata but adding the ‘expiry time’ parameter:

Module 5 – Updating and Deleting data using CodeIgniter 9


Web Development

The $array session variables will expire in 5 seconds

Module 5 – Updating and Deleting data using CodeIgniter 10

You might also like