cakephp checkbox naming patterns - cakephp

(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

Related

why can't getting value from wordpress database nextgen gallery in selection tag dropdown

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>

iMacros: Script: Split array on

iMacros Script
I Need to extract 3 radio buttons text, evaluate it against a stored option. So that the correct selection can be made. There are only 3 buttons but the options are dynamic. I can extract text, and if split can eval it with Gematria. My issue is splitting the text so it can be eval'd. The text is separated by
Example of the extraction SQF BOX
SQF BOX
Html I am pulling from
<td class="evenBand" width="60px" align="left">
<input type="radio" name="ordUom0" id="ordUomR0" tabindex="1001" value="SQF" onclick="checkHSUom(0,'SQF','BOX','','SQM',32.93,1.0,1712.36);"
>SQF
<br>
<input type="radio" name="ordUom0" id="ordUomI0" tabindex="1002" value="BOX" onclick="checkHSUom(0,'SQF','BOX','','SQM',32.93,1.0,1712.36);"
>BOX
<input type="hidden" name="orderedUom" id="orderedUom0" value=""
<="" td="">
</td>
DOES NOT WORK
SET !VAR3 EVAL("'{{!VAR2}}'.split(' ')[0].trim();")
SET !VAR5 EVAL("'{{!VAR2}}'.split(' ')[1].trim();")
SET !VAR6 EVAL("'{{!VAR2}}'.split(' ')[2].trim();")
DOES NOT WORK
SET !VAR3 EVAL("'{{!VAR2}}'.split(' ')[0].trim();")
SET !VAR5 EVAL("'{{!VAR2}}'.split(' ')[1].trim();")
SET !VAR6 EVAL("'{{!VAR2}}'.split(' ')[2].trim();")
Perhaps something like this will be helpful to you:
SET !VAR3 EVAL("'{{!VAR2}}'.split(/\s+/)[1];")
SET !VAR5 EVAL("'{{!VAR2}}'.split(/\s+/)[2];")

How do I make associated tables editable in the admin pages?

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.

How to populate drop-down list with database values in CakePHP

I am very new to CakePHP. Could you please explain me the steps needed to populate a select drop-down with values from the database. Please also suggest me some links to the reference.
Simple, if it's a related model in your controller you pass 'list' into the find(); an cake will make an id => value array for you, and the form helper will know exactly what to do with it.
For example say you want to get the list of categories for a product model, this is in your contoller:
$categories = $this->Product->Categories->find('list');
$this->set(compact('categories'));
Then in your view using the form helper, simply create the select element how you normally would any input:
$form->input('category_id');
The form helper will automatically load the $categories variable we set with $this->set().
You make a find in the db and then set the variable via $this->set(yourvariable) in the controller.
In the view you use the "yourvariable" in the select tag
Fill select 1
Fill select 2
Fill select 3
There is a cakeish way to do it with very less effort
$this->set('arrMain',$this->Post->find('list',
array(
'fields'=>array('id','title')
)));
Will give output as ==>
<select id="UserAge" name="data[User][Age]">
<option value="1">The title</option>
<option value="2">A title once again</option>
<option value="3">Title strikes back</option>
in the controller you do:
$this->loadModel('MyModel'); //if it's not already loaded
$options = $this->MyModel->find('all'); //or whatever conditions you want
$this->set('options',$options);
and in the view
<select...>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option['MyModel']['id']; ?>"><?php echo $option['MyModel']['field']; ?></option>
</select>
Example
in your controller Class
$categories = $this->Articles->find('list')->select(['id', 'title'])->toArray();
$this->set(compact('categories')); // pass result dataset to the view
In Your view file
<?php echo $this->Form->select('articles_categories_id', $categories); ?>
Reading Source
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

insert into database from Joomla form fields

I am beginner to Joomla! development and have created a very simple module.
How do I create a form with 3 text fields and then save the entered values into a database table?
Try this example:
We will Post a user's first and last name to a table.
create a table in your database. Note it should have the prefix "jos_"
We will call this form, "names". So we will name our table "jos_names"
At the SQL line in PHPMyAdmin (or whatever tool you use..), do this query to create a new table:
CREATE TABLE `databasename`.`jos_names` (`id` int(11) NOT NULL auto_increment, `firstname` VARCHAR(100), `lastname` VARCHAR(100), PRIMARY KEY (`id`) )
To simplify things, we will post the results to the same page.. Let's build the form:
<?php
/** post form to db module **/
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//--POST YOUR FORM DATA HERE-->
$fname = $_POST['fname'];
$lname = $_POST['lname'];
//--END POST YOUR FORM DATA---|
//--build the form------------>
?>
<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post">
<p><input type="text" name="fname" id="fname" value="" /></p>
<p><input type="text" name="lname" id="lname" value="" /></p>
<p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
//--END BUILD THE FORM--------|
<?
if( (isset($lname)) || (isset($fname)) ) {
//first name or last name set, continue-->
$data =new stdClass();
$data->id = NULL;
$data->firstname = $fname;
$data->lastname = $lname;
$db = JFactory::getDBO();
$db->insertObject('#__names', $data, id);
} else {
echo '<h4>One Field Is Required!</h4>';
}
?>
That should do it. If you are writing a traditional Joomla module, this should be your helper.php file.
NOTES:
Only include the "die" script once in a joomla document.. (defined( '_JEXEC' )..
JURI::current() automatically reads the current page URL. If you call echo JURI::current(); on a page with the url http://www.example.com/names, then it will display the same link.
It is important that the action="" points to the exact Url where you will publish this module.
Furthermore, it is considered bad practice to post data to 'SELF', but you are kindof limited with a module, so unless you build a component or a plugin, you should post your form to 'SELF' as done with this example. (JURI::current();)
When in the Joomla framework, it isn't necessary to declare your database name, username or password as Joomla is already "logged in".. So instead of querying databasename.jos__tablename, in joomla you can replace the query with this: #__tablename. In fact this is the best practice when dealing with db queries and Joomla because users do not have to use the default jos_ prefix, joomla automatically replaces "#" with whatever the prefix. In my case "#" equals "jos"
Take note when querying the sql to create the table.. make sure you replace databasename with the actual name of your database..
That should do it.
If for some reason you are unable to post data:
1) Make sure the form doesn't redirect to a different page when you click submit. If it does, change the form action"" to the absolute url to where this page is published.. then go from there.
2) Sometimes the $data =new method doesn't work. This depends on how you set up your module, functions and classes.
Here is an alternative:
$db =& JFactory::getDBO();
$query = "INSERT INTO `#__names` (`fname`, `lname`)
VALUES ($fname, $lname);";
$db->setQuery( $query );
$db->query();
Try something like http://www.chronoengine.com/ - Chronoforms
You may run a custom query to get a result on tmpl file which is simple
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$category = $_REQUEST['category'];
if(isset($category))
{
$db = JFactory::getDbo();
$db->setQuery("SELECT machine_id FROM j25_machinefinder_products WHERE category = '$category'");
// Load the row.
$result = $db->loadRowList();
//your result will return here
print_r($result);
}
?>
<form action="" method="get" name="usedequipment">
<select name="category">
<?php foreach($hello as $category)
{
?><option value="<?php echo $category[0]; ?>"> <?php echo $category[0]; ?></option><?php
} ?>
</select>
<input type="submit" />
</form>

Resources