You are here: cakephp » submitting an array into a database for multiple entries
Submitting an array into a database for multiple entries
- Written By
- Shadow
- Submitted At
- 2010-10-22 08:28:03
- Num Views
- 802
- Category
- CakePHP
|
Hi, I am struggling with a piece of code and was wondering if you could assist. It is for assigning a score to each entry at a show, some will be null and some will obviously have a score. The result I get is that it just saves the last entry into the database. [u]This is my controller:[/u] class PointsController extends AppController { public $name = 'Points'; function horses($class_id='') { $this->loadModel('Point'); $this->loadModel('EntriesType'); $this->loadModel('Entries'); $this->set('entries',$this->Entries->findAll()); $this->set('type',$this->EntriesType->findAll()); $this->set('class_id',$class_id); if($this->data){ if($this->Point->save($this->data)){ $this->Session->setFlash('The points were updated'); }else{ $this->Session->setFlash('There was a problem saving this information'); } } } [u]And this is my view:[/u] <form action="<?=$html->url('/points/horses')?>" method="post"> <fieldset> <legend>Award Points</legend> <table class="points"> <tr> <td> <h4>Show Number</h4> </td> <td> <h4>Stud Name</h4> </td> <td> <h4>Horse Name</h4> </td> <td> <h4>Owner</h4> </td> <td> <h4>Placement</h4> </td> </tr> <?php foreach($type as $types){ if($class_id == $types['EntriesType']['type_id']){ foreach($entries as $entry){ if($types['EntriesType']['entry_id'] == $entry['Entries']['id']){ $h_id[] = $entry['Entries']['id']; $t_id = $types['EntriesType']['type_id']; echo '<tr><td>', $entry['Entries']['horse_number'], '</td><td>', $entry['Entries']['horse_breeder'], '</td><td>', $entry['Entries']['horse_name'], '</td><td>', $entry['Entries']['owner'], '</td><td>', $form->input('Point.placement', array('label' => '')), '</td></tr>'; echo $form->input('Point.horse_id', array('type'=>'hidden', 'value'=> $_hid)), $form->input('Point.type_id', array('type'=>'hidden', 'value'=> $t_id)); } } } } ?> </table> <input type="submit" name="submit" value="Submit" /> </fieldset> </form> Thanks in advance! By Shadow @ 2010-10-22 08:28:03
|
|
Just saw my post has an error in it, $_hid is supposed to be $h_id.. By Shadow @ 2010-10-22 13:54:05
|
|
From what I can see you are just looping through the entries in the view and not in the controller when you save it. There is two options you can consider here. 1. You can change the view to have a form inside the loop with a save button for each of the entries. This will then only save the section you are clicking save on though. This is not a 100% working example but will be something like this: <fieldset> <legend>Award Points</legend> <table class="points"> <tr> <td> <h4>Show Number</h4> </td> <td> <h4>Stud Name</h4> </td> <td> <h4>Horse Name</h4> </td> <td> <h4>Owner</h4> </td> <td> <h4>Placement</h4> </td> </tr> <?php foreach($type as $types){?> <form action="<?=$html->url('/points/horses')?>" method="post"> <?php if($class_id == $types['EntriesType']['type_id']){ foreach($entries as $entry){ if($types['EntriesType']['entry_id'] == $entry['Entries']['id']){ $h_id[] = $entry['Entries']['id']; $t_id = $types['EntriesType']['type_id']; echo '<tr><td>', $entry['Entries']['horse_number'], '</td><td>', $entry['Entries']['horse_breeder'], '</td><td>', $entry['Entries']['horse_name'], '</td><td>', $entry['Entries']['owner'], '</td><td>', $form->input('Point.placement', array('label' => '')), '</td></tr>'; echo $form->input('Point.horse_id', array('type'=>'hidden', 'value'=> $_hid)), $form->input('Point.type_id', array('type'=>'hidden', 'value'=> $t_id)); } } }?> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?> </table> </fieldset> </form> OR 2. You can update the view to have be able to save multiple items, but not 100% sure what should be happening so not able to give you an example. Let me know if this is what you wanted? Alternatively also attach a screenshot of how the frontend looks so I have an better idea of what you want to achieve. By PHPin24 @ 2010-10-26 13:26:18
|
|
I also thought of submitting each one individually, but then human error could come into play, if the person should forget to hit submit. So ideally, I would like for all the points awarded, to write to the database simultaneously. Along with the horses' id and the type id. I have mailed the frontend snapshot to you. Hope this is a little more descriptive. Thanks By Shadow @ 2010-10-30 08:18:50
|
|
What about you grey out all the other inputs once they start entering in one of the blocks? Alternatively you could do a manual query in cake. With cake it is quite easy. In your model: $sql = 'INSERT INTO ....'; $result = $this->query($sql); Do a print_r of your $_POST variable when you hit submit. Also give me the table structure where the data should go into then we can do the SQL query here. :) By PHPin24 @ 2010-11-11 07:15:35
|
|
Ok, so... I've changed my view from: $h_id[] = $entry['Entries']['id']; and: echo '<tr><td>', $entry['Entries']['horse_number'], '</td><td>', $entry['Entries']['horse_breeder'], '</td><td>', $entry['Entries']['horse_name'], '</td><td>', $entry['Entries']['owner'], '</td><td>', $form->input('Point.placement', array('label' => '')), '</td></tr>'; echo $form->input('Point.horse_id', array('type'=>'hidden', 'value'=> $_hid)), $form->input('Point.type_id', array('type'=>'hidden', 'value'=> $t_id)); to: $h_id[] = $entry['Entries']['id']; and: echo '<tr><td>', $entry['Entries']['horse_number'], '</td><td>', $entry['Entries']['horse_breeder'], '</td><td>', $entry['Entries']['horse_name'], '</td><td>', $entry['Entries']['owner'], '</td><td>', $form->input('Point.placement', array('name'=>'data[placement][]', 'label' => false, 'options' => array( '0'=>'No Placement', '5'=>'1st', '4'=>'2nd', '3'=>'3rd', '2'=>'4th', '1'=>'5th', ) ) ), '</td></tr>'; echo $form->input('Point.horse_id', array('name'=>'data[horse_id][]', 'type'=>'hidden', 'value'=> $h_id)), $form->input('Point.type_id', array('name'=>'data[type_id][]', 'type'=>'hidden', 'value'=> $t_id)); This, of course, does not save to the DB at all. I know what the problem is, the solution however, not so much... :( When I hit submit, the debug($this->data); result that I get is: Array ( [placement] => Array ( [0] => 5 [1] => 4 ) [horse_id] => Array ( [0] => 31 [1] => 28 ) [type_id] => Array ( [0] => 1 [1] => 1 ) ) The DB table's name is: points There are 4 rows in the table, the names are the same as the debug array fields (placement, horse_id, type_id) and also a id row. Thanks! :p By Shadow @ 2010-11-11 10:29:43
|
