Friday, 16 January 2015

Updating Table data

Updating Table data
For Updating, we need to do the following changes.
Go to the index/User.phtml and make the following changes
<?php foreach ($this->results as $item):?>
            <h1><?php echo $item['firstname'];?></h1>
            <h3><?php echo $item['lastname'];?></h3>
            <span><?php echo $item['email'];?></span><br/><br/>
<a href="<?php echo $this->baseUrl();?>/index/delete/<?php echo $item['id'];?>">delete</a>
<a href="<?php echo $this->baseUrl();?>/index/update/<?php echo $item['id'];?>">Update</a>
<?php endforeach;?>

We have made only one change to the file that is to make a link to the updateAction()  with one parameter , that is the id of the record.
So as we click on the update link we will be directed to the updateAction() with the a  parameter of id. Now we have to make an Action called update and create some logic for updating table.
Go to indexController.php and make the following changes;
public function updateAction(){
    $data = $this->_getParam('id');
    $user = new Application_Model_DbTable_Users();
    $result = $user->getOneUser($data);
          $this->view->data = $result;
         
          if (isset($_POST) && !empty($_POST)) {
              $id = $_POST['id'];
              $array = array(
                        'firstname' => $_POST['firstName'],
                        'lastname' => $_POST['lastName'],
                        'email' => $_POST['email'],
                        );
              $user->updateData($array,$id);
              $this->redirect('index/user');
          }

Here we first get the id param and send it to the Dbtable to get the related data from the database, than we send this data to the update.phtml view.
Than in the second part we check if the post is submitted, if it is we formed an array and send it to the controller, the array is formed from the form data that is submitted.
Here is the view code in user.php

public function getOneUser($data){
    $row = $this->find($data);
    return $row->toArray();
    }

public function updateData($array,$id){
     $where = $this->getAdapter()->quoteInto('id = ?', $id);
    $this->update($array, $where);
    }
There are two functions that we create in user.php,  one is getting the form data from the table and second is the updateData function for updating the table.
And last if the code for view file update.phtml
<form action="<?php echo $this->baseUrl('index/update');?>" method="post"><dl class="zend_form">
<input type="hidden" name="id" value="<?php echo $this->data[0]['id']?>">
<dt id="firstName-label"><label for="firstName" class="required">First name</label></dt>
<dd id="firstName-element">
<input type="text" name="firstName" id="firstName" value="<?php echo $this->data[0]['firstname'];?>"></dd>
<dt id="lastName-label"><label for="lastName" class="required">Last name</label></dt>
<dd id="lastName-element">
<input type="text" name="lastName" id="lastName" value="<?php echo $this->data[0]['lastname'];?>"></dd>
<dt id="email-label"><label for="email" class="required">Email address</label></dt>
<dd id="email-element">
<input type="text" name="email" id="email" value="<?php echo $this->data[0]['email'];?>"></dd>
<dt id="submit-label">&nbsp;</dt><dd id="submit-element">
<input type="submit" name="submit" id="submit" value="Contact us"></dd></dl></form>
Here is a simple html form where the values are set to that send by the updatAction.

In the Process


When we first click the update link, we are headed towards the updateAction() where we get the id from the url,send this id to the User.php to get the data from the table. After getting the data from the table we pass the data to the view. In the view we may be made some changes to the data and then submit the form to the updataAction where the data is further processed and passed to the dbtable file to update the table.
 There is one more thing we need to do that is to make another route for update in index.php file;
Just like we do for deleteAction to get the param.
$router->addRoute(
          'update',
          new Zend_Controller_Router_Route('/index/update/:id',array('controller'=>'index','action'=>'update'))
);

Here we just another route to be used when we updating the data.

No comments:

Post a Comment