Professional Documents
Culture Documents
App Dev Lesson 5 PDF
App Dev Lesson 5 PDF
Topics
Introduction .................................................................................................................................. 2
Flashdata ................................................................................................................................................ 8
Tempdata ............................................................................................................................................... 9
Learning Objectives
1. Understand and apply the concept of Updating and Deleting Records in CodeIgniter
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.
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.
$data[“flowers”] contains array of records using the getFlowers() method from the model at
application/models/flowers_model.php
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
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
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
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
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.
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
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.
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.
This code will return a Boolean value of TRUE/FALSE of the delete query
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
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
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:
set_flashdata is used to set the flashdata session variable and it require 2 parameters: the
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:
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: