====== TaniPHP v0.9 tutorial #2 - Editing the students table ====== ===== Creating an "edit" action ===== In the previous example, we have created an URL that linked to ''edit'' action of our ''Students'' controller and also passed the ''id'' of the student. To show the edit form, we need to get the data about that student first. Add this code to ''students_controller.php'': function edit() { // create an instance of student with id == $_GET['id'] $student = new Student($_GET['id']); // load our HTML helper (you might not need this line in future releases) require_once("taniphp/html.php"); // pass the object to the view $this->view->set("student", $student); } ===== Adding the view for edit ===== Now we will create a view for our ''edit'' action. Following the [[:general#conventions|conventions]], the view will be saved in ''views/students/edit.php'':

Editing student details

The ''HTML::printForm()''((HTML::printForm() - [[taniapi>HTML|docs]], [[tanisvn>taniphp/html.php|source]])) method generates an edit form being given the instance of our model class. The form is colored using ''tr.color1'' and ''tr.color2'' set in ''[[tanisvn>files/style.css|files/style.css]]''. If you wish to futher customize your form, but maintain the usability of autogenerated fields, you should take a look at ''HTML::getForm()'' method that returns an array of tags that you can print using your own markup. You should note that no input tags were created for ''created_on'', ''updated_on'' fields. These are special fields and how the framework is treating them is described in the [[:general#conventions|conventions]] section. As for the ''id'' field, you should add it yourself if you wish to create an edit form (the HTML class generates tags that could be used both in edit/new forms). ===== Creating a "save" action ===== Add the ''save()'' method to ''students_controller.php'' file: function save() { $student = new Student; // assign vars we got from the form $student->assign($_POST); // try to update the table if($student->update()) { // everything ok, show the list of students again Framework::redirect('students','index'); } else { // something went wrong Framework::croak("Could not save!"); } }