在蛋糕中编辑多个具有一个表单的行 [英] Edit multiple rows with one form in cake

查看:107
本文介绍了在蛋糕中编辑多个具有一个表单的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的蛋糕应用程序中,我有一个名为faqs的模型,控制器叫faqs_controller&视图名为faqsindex.php。



我正在制作CMS,因此用户可以更改常见问题。数据库表'faqs'有5列id,类别,问题,答案和数字。 Number是常见问题的出现顺序。



列出所有常见问题的循环看起来或多或少如下:

 <?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; ?>

我想让用户可以从此屏幕更改number



你知道,像netflix的队列一样工作,用户可以从列表中重新排序它,您不必点击要查看的电影即可更改其队列中的顺序。



编辑我在faqs_controller.php中设置了编辑器,如下所示: / p>

  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 $ form-> input('Faq。'。$ key。'。question',array('value'=> $ value ['Faq'] ['question'])); >

以预填充表单。我不知道是如何更新正确的行。如果我提交表单我得到8 mysql查询像这样:



INSERT INTO faqs

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


In my cake app I have a model called faqs, controller called faqs_controller & view called faqsindex.php.

I'm making a CMS so users can change the FAQs. The db table 'faqs' has 5 columns id, category, question, answer and number. "Number" is the order in which the FAQ's will appear.

The loop that lists all of the FAQs looks more or less 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 so that the user can change the "number" cell from this screen, instead of going into a separate edit screen for each row and changing the number there.

You know, like how netflix's queue works, where the user can reorder it from the list, you don't have to click on the movie you want to see to change its order in your queue.

EDIT I set up edit in faqs_controller.php like this:

    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');
       }
}

and in the index view I made a foreach that looks like this:

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');
}

In this case the foreach goes round 8 times because there are 8 rows. If I submit them, I create 8 new rows & can't update existing rows.

-EDIT-

I changed the form echo here:

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

to prepopulate the form. What I can't figure out is how to update the proper row. If I submit the form I get 8 mysql queries like this:

INSERT INTO faqs (question) VALUES ('THE NEW QUESTION I JUST ADDED?') when I don't want an insert but an update.

解决方案

Put each faq id inside your form?

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');
}

这篇关于在蛋糕中编辑多个具有一个表单的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆