There are two tables in our database that are important for this question. In Orders, we have the following fields:
ID, account_id, sale_id, site_id, pending, order_date
The other related table, orders_product_types, is an associative table that contains
order_id, product_id, quantity
Cake originally baked an admin page for orders that works just as you would expect. However, the client has requested that we add the ability to edit the quantity of each product associated with the sale on that same orders.admin_edit page, as well as see the products on the admin_view page. I have already managed to get the view page to display (albeit in an unpretty way) with the following code. To the controller, I added
$order = $this->Order->findById($id);
$this->recursive = 1;
to the controller and included it in the set, and I display them on the view page with this:
<?php foreach ($order['ProductType'] as $productType){
echo $productType['type'];
echo $productType['OrdersProductType']['quantity'];?>
<br></br>
<?
}?>
However, now I'm stuck on how to do this in the cake form for editing. Ideally, I would like to find a way to do this using proper cake functions, but I'm still new to this and am still figuring out how everything works. The pertinent part of the form that allows them to edit the information for the order looks like this:
<?php echo $this->Form->create('Order'); ?>
<fieldset>
<legend><?php echo __('Edit Order'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('account_id');
echo $this->Form->input('sale_id');
echo $this->Form->input('site_id');
echo $this->Form->input('pay later');
echo $this->Form->input('order_date');
echo $this->Form->input('Coupon');
echo $this->Form->input('ProductType');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Ideally, I would like to find some way to follow this syntax, but to include fields from the associated table orders_product_types so that they can edit the quantities directly in this form. How would I include a second table within the cake structure?
I was unable to find a way to do it "properly" with cake syntax like I was looking for, but I did eventually solve the problem by inserting a table in the view as follows:
<table>
<? foreach($productTypes as $id => $productType):?>
<tr class="product">
<td><?= $productType ?></td>
<td class="price" id="<?= $id ?>"></td>
<td>
<select name="data[Quantity][<?=$id?>]" class="quantity">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<td class="subtotal"></td>
</tr>
<? endforeach; ?>
<tr>
<td colspan="4" style="text-align: right" id="total"></td>
</tr>
</table>
If anyone has a proper way to do it, please let me know. I am still learning my way around cake, and every little bit helps.
Related
Hy all I have this code this is printing the array in the result but not getting value in the selectin dropdown I want to get the nextgen gallery names from the database! Tell me where I am wrong
<select>
<?php
foreach($gallerylist as $galleryrow) {?>
<?php echo $galleryrow; ?>
<option value="<?php echo $galleryrow; ?>"><?php echo $galleryrow; ?></option>
<?php}
?>
</select>
global $wpdb;
$gallerydefine= $wpdb->prefix."ngg_gallery";
$query="SELECT * FROM $gallerydefine";
$gallerylist = $wpdb->get_results($query);
Try to read your code as a book:
First, you're trying to print a list of galleries (which you don't have, it's an undefined variable) as a selectbox:
<select>
<?php
foreach($gallerylist as $galleryrow) { ?>
<?php echo $galleryrow; ?>
<option value="<?php echo $galleryrow; ?>"><?php echo $galleryrow; ?></option>
<?php } ?>
</select>
After that, you're loading the list of galleries from the database, but you're not using it for anything, you just load it and then the script ends:
global $wpdb;
$gallerydefine= $wpdb->prefix."ngg_gallery";
$query="SELECT * FROM $gallerydefine";
$gallerylist = $wpdb->get_results($query);
Obviously, it doesn't make any sense. It's like having a lunch where you first "eat it" (even though you don't have it) and then you go buy ingredients, cook it and go sleep without actually eating it.
Just think about what you're doing it put everything in the right order.
Important notes
enable reporting and displaying of all errors including warnings and notices on your local development environment. If you would have it enabled, you know immediately you're doing something wrong: it would tell you you're using an undefined variable $gallerylist inside the foreach loop.
Also, it's a good idea to check if there hasn't been any database error right after you make a database query.
Always check what HTML code is your script generating. You may see just a blank web page but there may be a lot of HTML code behind that. You're for example printing text between HTML tags <select> and <option> which is not valid.
And use a debugger and go through your script step by step. You'll understand in what order the statements are being executed, what values variables contain in a particular point in the time, etc.
Your code is out of order. Change it to:
<?php
global $wpdb;
$gallerydefine = $wpdb->prefix."ngg_gallery";
$query="SELECT * FROM {$gallerydefine}";
$gallerylist = $wpdb->get_results($query);
?>
<select>
<?php
foreach($gallerylist as $galleryrow) {?>
<option value="<?php echo $galleryrow->gid; ?>"><?php echo $galleryrow->name; ?></option>
<?php}
?>
</select>
I am new to cakephp framework and i am using cakephp3.0 now i used baking concept instead of using scaffolding. After baking it automatically generated all pages based on my tables in database and it generated code in models,controllers and views.Now my question is "if it is possible to change the code in views to change field types (from text box to radio button) according to my requirements."
Please help me.
Thanks in advance
Yes, after you bake your project you can change the fields types. Navigate to the folder where all your views are located, for example: app\View\MyViewName. Baking is a great tool to get something up and running fast. If you have a fairly structured website mostly used for data entry this is a great tool. I used it for simple data entry websites and it has saved me so much typing! Just add some data validation/constraints in the model and you're good to go!
A freshly baked view will look a little something like this:
<div class="MyForm Form">
<?php echo $this->Form->create('MyForm'); ?>
<fieldset>
<legend><?php echo __('Add My Form'); ?></legend>
<?php
echo $this->Form->input('field1');
echo $this->Form->input('field2');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List My Forms'), array('action' => 'index')); ?></li>
</ul>
</div>
Change the input methods to look something like this:
$options = array('Y' => 'Yes', 'N' => 'No');
echo $this->Form->radio('myFields', $options);
(Edited to provide clearer example)
I have cakephp model structure of document->section->variables
In a form I want to be able to list all variables, but nested within document and then section levels. That bit is fine.
I want check boxes alongside each item at the variable level - to allow selection of variables.
I want a checkbox at sectionlevel, that when selected will select all the checkboxes for the variables in that section.
I want a checkbox at document level so that when selected it will select all checkboxes for the variables for that document (ie in all sections.)
To facilitate this I have named the checkboxes thus (well, I have set the ids)(pseudo code):
document level: id = Document id
section level id = Document id_Section id
variable level id = Document id_Section id_Variable_id
My hope this that if I select the section level checkbox I can set all the variable level checkboxes FOR THAT SECTION by using the Document id_Section_id stub at the beginning of all the ids for the variable level checkboxes... but maybe I am grasping at straws?
Here is a sample of checkboxes in levels: (apologies for layout)
<?php
//Document level
//all of this within a ForEach loop for documents
echo "<table>";
echo "<tr><td>Collection dates</td><td>".$project_document['data_collection_dates']."</td>";
echo "<td>Select (toggle) all variables"
.$this->Form->checkbox($project_document['id'], // note that this sets the chkbox id to the value of the current document id
array('hiddenField' => false,'onclick'=> 'SelectSection(this,this.name,this.checked);'))
."</td></tr>";
echo "</table>";
foreach ($project_document['ProjectDocumentSection'] as $project_document_section):
echo "<h4>" . $project_document_section['title']. "</h4>";
echo "<table>";
echo "<tr><td>Collection Method</td><td>" . $project_document_section['method']. "</td></tr>";
echo "<tr><td>Collection objective</td><td>" . $project_document_section['objective']. "</td>";
echo "<td>Select (toggle) all section variables"
.$this->Form->checkbox($project_document['id']
."_".$project_document_section['id'] // and here the chkbox id is set to combination of document id and section is (using _ as separator)
, array('hiddenField' => false))."</td></tr>";
echo "</table>";
echo "<table><tr><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th></tr>" ; // header for variable table
foreach ($project_document_section['ProjectDocumentSectionVariable'] as $project_document_section_var):
echo"<tr><td>".$project_document_section_var['variable_type']
."</td><td>".$project_document_section_var['variable_format']
."</td><td>";
echo "<td>".$this->Form->checkbox($project_document['id']
."_".$project_document_section['id']
."_".$project_document_section_var['id'], array('hiddenField' => false))."</td></tr>";
endforeach;
echo "</table>";
endforeach;
// and later endforeach for the document itself
?>
And when rendered this might look something like this;
<td>Select (toggle) all variables
<input type="checkbox" name="data[Project][11]" onclick="SelectSection(this,this.name,this.checked);" value="1" id="Project11"/>
// note that checkbox id and name include ref to document id (11)
</td></tr>
<h4>Details of Health Workers at Facility</h4>
<table>
<tr><td>Collection Method</td><td>FW completed evaluation on paper</td></tr>
<tr><td>Collection objective</td><td>blah blah blah blah</td>
<td>Select (toggle) all section variables
<input type="checkbox" name="data[Project][11_24]" value="1" id="Project1124"/></td></tr>
// note that checkbox id and name include ref to document id (11) and section id (24)
</table>
<table>
<tr><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th></tr>
<tr><td>Site</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_402]" value="1" id="Project1124402"/></td></tr>
// note that checkbox id and name include ref to document id (11), section id (24) and variable id (402)
<tr><td>Facility</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_403]" value="1" id="Project1124403"/></td></tr>
<tr><td>Name</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_404]" value="1" id="Project1124404"/></td></tr>
<tr><td>Position of Health Worker</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_405]" value="1" id="Project1124405"/></td></tr>
<tr><td>Description</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_406]" value="1" id="Project1124406"/></td></tr>
<tr><td>Interviewer Code</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_407]" value="1" id="Project1124407"/></td></tr>
<tr><td>Date of Facility Visit </td><td>Date</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_408]" value="1" id="Project1124408"/></td></tr>
</table>
Has anyone any advice on how to approach this? I am sure there is more than one way to kill this bird..
Thanks
Paul
IF I understand your question, just use the FormHelper to create your checkboxes - they'll include the model in their id, and you can then upon click use Javascript (jQuery makes it easy) to modify checkboxes that contain '_variable', or '_section'...etc
I'm very new to CakePHP (and only slightly less new to MVC concepts) and am trying to build a system with a HABTM relationship between events and participants. I'd like the index page to display a list of events in a table, with one cell of each row containing a full list of participants. So far, my table display loop code looks like:
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['Event']['id']; ?></td>
<td><?php echo $event['Event']['title']; ?></td>
<td><?php
foreach ($participants as $participant):
echo $participant['Participant']['name'];
endforeach;
?></td>
</tr>
<?php endforeach; ?>
I know this is wrong, but I've tried many variations on this theme (all of which seem to my mind as equally wrong), such as $event['Participant']... and nothing works. I know the answer is simple, but I don't know what it is and searching around just gives answers to similar, but not sufficiently similar, answers. What do I need to write?
I would do something like this considering recursive IS NOT 0 in your controller.
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['Event']['id']; ?></td>
<td><?php echo $event['Event']['title']; ?></td>
<td><?php
foreach ($event['Participant'] as $participant):
echo $participant['name'];
endforeach;
?></td>
</tr>
<?php endforeach; ?>
This is untested. So if you need anymore help on this solution, please let me know.
In my cakephp app I have an Option model.
In my option/index view I display 2 options with inputs and radio button fields.
I want to update both of them, but I get a weird behaviour.
The option I alter doesn't get saved and instead a new option is inserted with the new value.
Here is my view
<h2 class='page-title' id='manage-options'>Opzioni</h2>
<?php echo $form->create(null, array('action'=>'index')); ?>
<table>
<tr>
<td><?= $options[0]['Option']['name']?></td>
<td><?= $form->radio(
$options[0]['Option']['id'],
array(
'1' => 'Sì',
'0' => 'No'),
array('default'=> $options[0]['Option']['value'], 'legend'=>false)
);?>
</td>
</tr>
<tr>
<td><?= $options[1]['Option']['name']?></td>
<td><?= $form->input($options[1]['Option']['id'],array('label'=>false,'value' => $options[1]['Option']['value'] ))?></td>
</tr>
</table>
<?php echo $form->submit('Salva'); ?>
<?php echo $form->end(); ?>
And my controller:
function index() {
if (!empty($this->data)) {
foreach($this->data['Option'] as $id => $value) :
$this->Option->id = $id;
$feedback = $this->Option->read();
$this->Option->saveField('value', $value);
endforeach;
$this->Session->setFlash('Opzioni aggiornate');
}
$this->Option->recursive = 0;
$this->set('options', $this->paginate());
}
Before posting here I spent two hours googling for answers and experimenting. I know about saveAll() and i have tried these solutions:
http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms
http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/
I have been tweaking my code to fit these patterns, but I got no results (oscillating between 'not working' and 'not working and I get an extra record'), so I decided to post my original code.
Can you help, indicating the most proper way to do this?
Cheeers,
Davide
The problem was with the data in the DB. The kind folks on the cakephp IRC channel called my attention to the fact that in most databases ID = 0 equals 'new record'. For some reason I had an option with ID 0, so when updating the underlaying mysql query actually created a new record.
Changed the IDs, problem fixed.
The main problem with your code that I see is that your fields, both the radio and the input, are being built with only an ID value as the first parameter. The correct "cake way" of building a field is having the first parameter be Model.fieldname, in your case I believe it would be $form->input('Option.id', array())?>
If you inspect the html generated by your code you will see the field name is data[id], and it should be data[Option][id] if you want to loop through $this->data['Option'] in your controller.
Try changing your code to include the Model.fieldname as the first parameter and then it should have the data submitted correctly to your controller.