Lets say we have this scenario we have table of

  • No School
  • AA 1
  • 140
  • 100% (2) 2 out of 2 people found this document helpful

This preview shows page 45 - 50 out of 140 pages.

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
Image of page 45
Web Development Module 5 Updating and Deleting data using CodeIgniter 3 getFlowers() can be used to fetch a single or a bunch of records. Flower Listings view at application/views/flowers.php
Image of page 46
Web Development Module 5 Updating and Deleting data using CodeIgniter 4 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 :
Image of page 47
Web Development Module 5 Updating and Deleting data using CodeIgniter 5 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.
Image of page 48
Web Development Module 5 Updating and Deleting data using CodeIgniter 6 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.
Image of page 49
Image of page 50

You've reached the end of your free preview.

Want to read all 140 pages?

  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes