Zend Framework 1 Update Action

206 views Asked by At

I've got a very annoying problem. By clicking the "Update" button I always get the same result - only the very last element is being edited. Unfortunately I can't find the solution. Thanks for your help.

This is the view part:

<form id="edit-action" enctype="multipart/form-data" action="<?php echo $this->baseUrl; ?>/admin/editflight" method="post">
    <?php
        for ($i=0; $i < count($this->flight); $i++) { 
            echo "<div class='edit-action-box'>".
                     "<input type='hidden' name='id' value='".$this->flight[$i]->id."' />".
                     "<input type='text' name='name' value='".$this->flight[$i]->name."' />".
                     "<input type='text' name='date' value='".$this->flight[$i]->date."' />".
                     "<textarea name='description'>".$this->flight[$i]->description."</textarea>".
                     "<input id='edit-button' type='submit' value='Edit' />".
                 "</div>";
            }
    ?>
</form>

And this is the controller part:

function editflightAction() {

    if ($this->_request->isPost()) {
        Zend_Loader::loadClass('Zend_Filter_StripTags');
        $filter = new Zend_Filter_StripTags(); 
        Zend_Loader::loadClass('flight');
        $flight = new flight();

        $id = (int)$this->_request->getPost('id');

        $name = $filter->filter($this->_request->getPost('name'));
        $name = trim($name);

        $date = $filter->filter($this->_request->getPost('date'));
        $date = trim($date);

        $description = $filter->filter($this->_request->getPost('description'));
        $description = trim($description);

        $data = array(
            'name'   => $name,
            'date'   => $date,
            'description' => $description,
        );
        $where = 'id = ' . $id;
        $flight->update($data, $where); 

        $this->_redirect('/admin/panel#flight');
        return;
    }
}
1

There are 1 answers

2
venca On

First you have to correct your html. Add indexes to form elements.

Eg. "<input type='text' name='name' value='".$this->flight[$i]->name."' />" becomes "<input type='text' name='name[" . $i . "]' value='".$this->flight[$i]->name."' />"

Then in create loop for ($i=0; $i < count($this->flight); $i++) { in your controller to access indexed post data.