views:

42

answers:

1

Hi,

I'm getting to grips with Zend_Form and can create a form to add/edit a single database record, but now I'm trying to create a form to edit multiple records on a single page. For example, if I had a table of sports teams, and another table of players assigned to them teams I would want to be able to click on a team on my site and have all the players listed as rows with inputs to edit their names, date of births etc., with one submit button at the bottom to save any changes.

One thing to note is that there are a variable number of records that could be edited on a page; it is not a set amount.

Any pointers/help would be very much appreciated.

Thanks.

A: 

I use the code below to delete multiple items from the database.

On the index page (list of my database records):

<form method="post" action="<?php echo $this->baseUrl().'/admin/pages/deleteMultiple'; ?>">
    <td class="checkboxTable"><input name="remove[<?php echo $data[$row]->id; ?>]" type="checkbox" id="checkbox[]" value="<?php echo $data[$row]->id; ?>"/></td>
    <input class="deleteMultipleButtonBottom" name="deleteMultiple" type="submit" id="deleteMultiple" value="<?php echo $this->translate('Delete selected'); ?>">
</form>

The user sees a confirmation page:

  <form method="post">
        <input type="hidden" name="removeId" value="<?php echo implode($_POST['remove'], ','); ?>" />
        <input class="deleteYes" type="submit" name="deleteMultiple" value="Yes" />
        <input class="deleteNo" type="submit" name="deleteMultiple" value="No" />
    </form>

And my action looks like this:

if($this->getRequest()->isPost())
        {
            if($this->getRequest()->isPost('remove'))
            {
                $this->view->pages = $this->pagesService->GetPages($this->getRequest()->getPost('remove'));

                if($this->getRequest()->getPost('deleteMultiple') == 'Yes')
                {
                    $this->pagesService->DeleteMultiplePages($this->getRequest()->getPost('removeId'), $this->view->user->username, $this->getRequest()->getPost('countedItems'));
                }
                elseif($this->getRequest()->getPost('deleteMultiple') == 'No')
                {
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('The pages were <u>not</u> deleted'), 'status' => 'notice'));
                    $this->_helper->redirectToIndex();
                }
            }
        }   

And in my service page:

public function DeleteMultiplePages($id)
    {
        $this->pages->delete('id IN (' . $id . ')');
    }

This approach should work for updating values.

Rick de Graaf