Editing multiple lines with one form in a cake

In my cake application, I have a model called faqs, the controller is called faqs_controller and the view is called faqsindex.php.

I am creating a CMS so that users can modify frequently asked questions. The db faqs table has 5 columns id, category, question, answer and number. "Number" is the order in which the FAQ appears.

The cycle that lists all the frequently asked questions looks something like this:

<?php
foreach ($faqs as $faq):
<tr>
<td><?php echo $faq['Faq']['category']; ?></td>
<td><?php echo $faq['Faq']['number']; ?></td>
<td><?php echo $faq['Faq']['question']; ?></td>
<td><?php echo $faq['Faq']['answer']; ?></td>
</tr>
<?php endforeach; ?>

I want to make it possible for the user to change the cell "number" on this screen, instead of going to a separate editing screen for each line and changing the number there.

, Netflix, , , , .

edit faqs_controller.php :

    function edit() {
       if(!empty($this->data)) {
          $this->Faq->saveAll($this->data['Faq']);
       }
       else {
          $this->data['Faq'] = Set::combine($this->Faq->find('all'), '{n}.Faq.id', '{n}.Faq');
       }
}

foreach, :

echo $form->create('Faq', array('action'=>'edit'));
foreach($this->viewVars['faqs'] as $key => $value) {
    echo 'id:'.$value['Faq']['id'];
    echo '<br/>question:'.$value['Faq']['question']; 
    echo $form->input('Faq.'.$key.'.number');
}

foreach 8 , 8 . , 8 .

-Edit -

echo :

echo $form- > input ('Faq.'. $key. '. question', array ('value' = > $value ['Faq'] ['question']));

. , . , 8 mysql, :

INSERT INTO faqs (question) ( " ?" ), , .

+1
3

faq- ?

echo $form->create('Faq', array('action'=>'edit'));
foreach($this->viewVars['faqs'] as $key => $value) {
    echo 'id:'.$value['Faq']['id'];
    echo '<br/>question:'.$value['Faq']['question']; 
    echo $form->hidden('Faq.'.$key.'.id', array('value' => $value['Faq']['id']));
    echo $form->input('Faq.'.$key.'.number');
}
+4

jquery: http://jqueryui.com/demos/sortable/ , javascript, . script.

Edit:

echo $form->create('Faq', array('action'=>'edit'));
foreach($this->data['Faq'] as $key => $value) {
   echo 'id:'.$value['Faq']['id'];
   echo '<br/>question:'.$value['Faq']['question']; 
   echo $form->input('Faq.'.$key.'.number');
   echo $form->input('Faq.'.$key.'.id');
}
echo $form->end('Save');

:

function edit() {
   if(!empty($this->data)) {
      $this->Faq->saveAll($this->data['Faq']);
   }
   $this->data['Faq'] = Set::combine($this->Faq->find('all'), '{n}.Faq.id', '{n}.Faq');
}

.

+3

Cake, , saveAll().

function edit() {

    if (!empty($this->data)) {
        $this->Faq->saveAll($this->data['Faq'], array('conditions' => array('Faq.id' => $yourId)));
    }
    ...

}

This tells Cake ORM to keep the information in the line only where it Faq.idis equal $yourId. If it $yourIdmatches the existing identifier in your table, then this row should be updated.

Edit

You can put a field idin a hidden form element using CakePHP FormHelper .

$this->Form->hidden('id', array('value' => $value['Faq']['id']));
+1
source

All Articles