Checking if checkbox value isset when part of an array - Codeigniter - arrays

I have a dynamic form that is populated with check boxes. I'm trying to implement a permissions / roles system for my dashboard.
the form is populated depending on what permissions are stored in the db. for this example i only have 2. this is the code for the form :
if (!empty($permissions)) {
foreach ($permissions as $perm) {
?>
<tr>
<td><?php echo $perm->display_name; ?></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="view[]" id="<?php echo $perm->id ?>-view" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-view"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="edit[]" id="<?php echo $perm->id ?>-edit" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-edit"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="create[]" id="<?php echo $perm->id ?>-create" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-create"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="delete[]" id="<?php echo $perm->id ?>-delete" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-delete"> </label>
</div></td>
</tr>
<input type="hidden" name="permission_id[]" value="<?php echo $perm->id ?>">
<input type="hidden" name="permission_name[]" value="<?php echo $perm->permission_name ?>">
<?php
}
}
?>
the form works great and is displayed like this :
this is my controller code :
public function setPermissions()
{
if ($this->isAdmin() == true) {
$this->loadThis();
} else {
// GET FORM FIELD INPUTS
extract($_POST);
foreach ($permission_id as $key => $permission_id) {
$data = array(
'permission_id'=> $permission_id,
'permission_name' => $permission_name[$key],
'can_view' => $view[$key],
'can_edit' => $edit[$key],
'can_create' => $create[$key],
'can_delete' => $delete[$key],
'role_id' => $roleId,
);
$result = $this->settings_model->setPermissions($data);
}
}
if ($result > 0) {
$this->session->set_flashdata('success', 'Permissions set successfully');
} else {
$this->session->set_flashdata('error', 'Setting permissions failed');
}
// LOAD VIEW
redirect('staffPermissions');
}
and my model :
public function setPermissions($data)
{
$this->db->trans_start();
$this->db->insert('app_staff_permissions', $data);
$this->db->trans_complete();
return true;
}
i have tried the trick of putting a hidden input field above the checkbox's but this only returns a result for the last row added to the db.
The problem i having is when a check box is not checked i get an error that the variable is not declared. Presumably this is because it is returning a null value.
i have tried to check for a value like this :
'can_view' => ($view[$key]) == '1' ? '1' : '0',
'can_edit' => ($edit[$key]) == '1' ? '1' : '0',
'can_create' => ($create[$key]) == '1' ? '1' : '0',
'can_delete' => ($delete[$key]) == '1' ? '1' : '0',
but that also does not work and throws the same error. i have tried an if else statement within the array but that obviously does not work. I'm really stuck with this now and willing to try any suggestions.
Is there any way to check if the value is null and if so assign a '0' to it?
Edit :
i have tried hundreds of different ways to solve this problem and i think by using
'can_view' => isset($view[$i]) ? 'on' : 'off',
'can_edit' => isset($edit[$i]) ? 'on' : 'off',
'can_create' => isset($create[$i]) ? 'on' : 'off',
'can_delete' => isset($delete[$i]) ? 'on' : 'off',
i over come the variable's not being designed and it does write either 'off' or 'on' in the db.
However i get a strange behaviour that only the first row gets inserted. for example if i set my check-boxes (now changed to toggle switches) like this :
i would expect my database to look this
on | off | on | off ---- off | on | off | on
but instead this is what i get :
Another strange thing i have noticed is they are being inserted from the bottom, The first column 'id' is an AI column but the id's are incrementing backwards up the table.
The bottom row was the first one inserted. Now you can see that only the first row contains any of the checkbox's 'on' fields, the second is off or null. when we should see on, off, on, off etc.
I have tried including the db call within the foreach loop in controller and doing a regular $this->db->insert and have also tried removing it out of the for each and using $this->db->insert_batch
The other parameters permission_id and permission_name iterate through the loop fine, its as though something breaks when it gets to the checkbox data.

you can use isset to check if a variable is declared and is different than NULL.
'can_view' => isset(view[$key])? '1' : '0',
'can_edit' => isset($edit[$key])? '1' : '0',
'can_create' => isset($create[$key])? '1' : '0',
'can_delete' => isset($delete[$key])? '1' : '0',

Related

CakePHP 2 output checkbox array inside loop

I've read CakePHP multiple checkbox array HTML the right way but getting strange results. I have a list of tags (from a Model called Tag). I want to loop through these and output checkboxes for them in a View.
So I have obtained the Tag data in my Controller with:
$tags = $this->Tag->find('list', ['order' => ['name' => 'ASC']]);
$this->set('tags',$tags);
When I loop through it in my View I am trying to output the checkboxes in between Bootstrap markup:
<?php echo $this->Form->create('GroupTag'); ?>
<?php foreach ($tags as $tag_id => $tag): ?>
<div class="checkbox">
<label>
<?php echo $this->Form->checkbox('tag_id[]', array( 'value'=> $tag_id)); ?>
<?php echo $tag; ?>
</label>
</div>
<?php endforeach; ?>
I copied the syntax for tag_id[] from the post I linked to.
But when I inspect the markup it's producing the following as the name attribute for each <input type="checkbox">:
data[GroupTag][tag_id[]]
Should this not be
data[GroupTag][tag_id][]
?
The idea is that I have multiple checkboxes with a name attribute tag_id[] and then in the Controller I can loop through what has been checked.
Please can someone advise on this as I can't get it working and have looked into examples provided on here/docs.
Try this
$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
You can also do this:
$this->Form->input('GroupTag.tag_id', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $tag_id
]);
FormHelper::select(string $fieldName, array $options, array $attributes)
Example:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox'
));
Output:
<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" value="Value 1"
id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>
</div>
</div>

CakePHP 3.4 use setTemplates() multiple times

I'm trying to use setTemplates() multiple times in a view, but it doesn't work as expected.
I have two templates which I want to set when I need them.
$bootstrapTemplate = [
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
];
$bootstrapTemplateInputGroup = [
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
'input' => '<div class="input-group"><div class="input-group-addon">€</div><input type="{{type}}" name="{{name}}"{{attrs}}/></div>'
];
I start to set the templates like this
$this->Form->setTemplates($bootstrapTemplate);
$this->Form->control('title', ['class' => 'form-control', 'label' => __('Titel')]);
// OUTPUT - correct
// <div class="form-group text required"><label for="title">Titel</label><input type="text" name="title" class="form-control" required="required" maxlength="255" id="title"></div>
$this->Form->setTemplates($bootstrapTemplateInputGroup);
echo $this->Form->control('price', ['class' => 'form-control', 'id' => 'price_eur', 'label' => __('Preis EUR')]).'</div>';
// OUTPUT - correct
<div class="form-group number required"><label for="price_eur">Preis EUR</label><div class="input-group"><div class="input-group-addon">€</div><input type="number" name="price" class="form-control" id="price_eur" required="required" step="any"></div></div>
Now I want to switch back to $bootstrapTemplate which doesn't seem to work. Instead the $bootstrapTemplateInputGroup is used
$this->Form->setTemplates($bootstrapTemplate);
echo $this->Form->control('user_zip', ['class' => 'form-control', 'id' => 'user_zip', 'label' => __('PLZ')])
// OUTPUT - wrong
<div class="form-group text"><label for="user_zip">PLZ</label><div class="input-group"><div class="input-group-addon">€</div><input type="text" name="user_zip" class="form-control" id="user_zip"></div></div>
My expected output is the template of $bootstrapTemplate like:
<div class="form-group text required"><label for="user_zip">PLZ</label><input type="text" name="user_zip" class="form-control" required="required" maxlength="255" id="user_zip"></div>
What am I doing wrong here?
FormHelper::setTemplates() doesn't overwrite the complete existing set of templates, it merges it with the given templates, ie the input template set with the second call, will remain changed.
You either have to push() to (store) and pop() from (restore) the template stack using the underlying templater to avoid that:
$this->Form->templater()->push();
$this->Form->templater()->add($bootstrapTemplateInputGroup);
$this->Form->templater()->pop();
or use the FormHelper::create() method's templates option to apply the templates to the specific FormHelper::control() call(s) only:
echo $this->Form->control('title', ['templates' => $bootstrapTemplate, /*...*/]);
echo $this->Form->control('price', ['templates' => $bootstrapTemplateInputGroup, /*...*/]);
echo $this->Form->control('user_zip', ['templates' => $bootstrapTemplate, /*...*/]);
See also
Cookbook > Views > Helper > Form > Options for Control
API > \Cake\View\StringTemplate

Retrieve WordPress Database Info and display in form

Sorry I am new to WordPress and still having difficulty to retrieve my data from my custom database. So far this is my code for submitting data to my wp_datareg table.
<?php
/*
Template Name: Data-Register
*/
get_header();
?>
<?php
If($_POST['Submit']) {
global $wpdb;
$Data1=$_POST['Data1'];
$Data2=$_POST['Data2'];
if($wpdb->insert(
'wp_datareg',
array(
'Data1' => $Data1,
'Data2' => $Data2
)
) == false) wp_die('Database Insertion Failed');
else echo '<h2>Database Insertion Succesful</h2><p />';
?>
<?php
}
else // else we didn't submit the form so display the form
{
?>
<
<h4>Data Registration Form</h4>
<form action="" method="post" id="addcourse">
<p><label>Put Data1:<input type="text" name="Data1" size="30" /></label></p>
<p><label>Put Data2: <input type="text" name="Data2" size="30" /></label></p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
What I want to add is put another text form and search button where user can search a data of Data1 and edit its value on the form, (please see the image)
Sorry for being such a noob.
PLEASE SEE THIS IMAGE
Thank you.
br
We can fetch data from wp_datareg table using this code, however putting back the data to the form is my problem.
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_datareg" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->Data1;?></td>
</tr>

Yii dynamic checkbox

I have a column in my table named 'availability_option' with type enum('0', '1', '2'). Zero means 'Fixed Price', One means 'Auction', Two means 'Both'.
I want to generate 2 checkboxes dynamically One for Fixed Price and another 'Auction'.
How is it possible?
I didi it static.
But it should not be the right syntax of yii.
<input value="0" id="fixedprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="fixedprice">Fixed Price</label>
<input value="1" id="auctionprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="auctionprice">Auction</label>
I want it dynamically, So how is it possible?Any idea?
Use checkboxList():
<?= $form->field($model, 'attribute_name')->inline(true)->checkboxList([0 => 'Fixed Price', 1 => 'Auction']) ?>
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html#inline%28%29-detail
I was facing The same problem and I solved It by doing like this :
<?php echo $form->field($model, 'name[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
Note that the checkboxList expects the first argument of the passed array to be of string type as well so you may need to make changes to your array.
Generated HTML will be as:
<label><input type="checkbox" name="fomrmName[name][]" value="'1'"> a</label>
another way by which I solved My problem was to wrap the checkbox in foreach block like following:
<?php foreach($vendorsData as $value){?>
<li><?= $form->field($ucVendors, 'vendor_id['.$value.']')->checkbox(array('label'=>$value)); ?>
and the Generated html is as:
<label><input type="checkbox" id="usedcarvendors-vendor_id-value" name="UsedCarVendors[vendor_id][value]" value="1"> value</label>
Reference Link here

Chaging the label position for an input at CakePHP 2.2

CakePHP usually place labels before the input, so doing this:
echo $this->Form->input('subject');
We obtain this:
<div class="input text required">
<label for="TicketSubject">Subject</label>
<input name="data[Ticket][subject]" maxlength="255" type="text" id="TicketSubject">
</div>
Is there any way to place the label after the input to obtain this?
<div class="input text required">
<input name="data[Ticket][subject]" maxlength="255" type="text" id="TicketSubject">
<label for="TicketSubject">Subject</label>
</div>
Thanks.
The proper way is using the the 'format' option.
$this->Form->input('subject', array(
'format' => array('before', 'input', 'between', 'label', 'after', 'error')
));
Didn't anyone read the API :)
You can try this:
echo $this->Form->input('subject', array('label' => false, 'after' => $this->Form->label('Subject:')));
You can do like this also -
echo $this->Form->input('subject', array('label' => false, 'after' => '<label for="subject">Subject</label>'));

Resources