Note that there are some explanatory texts on larger screens.

plurals
  1. POEdit multiple rows with one form in cake
    text
    copied!<p>In my cake app I have a model called faqs, controller called faqs_controller &amp; view called faqsindex.php. </p> <p>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.</p> <p>The loop that lists all of the FAQs looks more or less like this:</p> <pre><code>&lt;?php foreach ($faqs as $faq): &lt;tr&gt; &lt;td&gt;&lt;?php echo $faq['Faq']['category']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $faq['Faq']['number']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $faq['Faq']['question']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $faq['Faq']['answer']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>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.</p> <p>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.</p> <p>EDIT I set up edit in faqs_controller.php like this:</p> <pre><code> function edit() { if(!empty($this-&gt;data)) { $this-&gt;Faq-&gt;saveAll($this-&gt;data['Faq']); } else { $this-&gt;data['Faq'] = Set::combine($this-&gt;Faq-&gt;find('all'), '{n}.Faq.id', '{n}.Faq'); } } </code></pre> <p>and in the index view I made a foreach that looks like this:</p> <pre><code>echo $form-&gt;create('Faq', array('action'=&gt;'edit')); foreach($this-&gt;viewVars['faqs'] as $key =&gt; $value) { echo 'id:'.$value['Faq']['id']; echo '&lt;br/&gt;question:'.$value['Faq']['question']; echo $form-&gt;input('Faq.'.$key.'.number'); } </code></pre> <p>In this case the foreach goes round 8 times because there are 8 rows. If I submit them, I create 8 new rows &amp; can't update existing rows. </p> <p>-EDIT-</p> <p>I changed the form echo here:</p> <p>echo $form->input('Faq.'.$key.'.question',array('value'=>$value['Faq']['question']));</p> <p>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:</p> <p>INSERT INTO <code>faqs</code> (<code>question</code>) VALUES ('THE NEW QUESTION I JUST ADDED?') when I don't want an insert but an update.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload