How to Save Multiple Check-boxes data to database? - database

I am not an expert so I'd really appreciate if you are real specific on your answers.
I have this registration form that has a section with lots of check-boxes, and I wondering what the best way is to save this in the database. I am not sure if all values should go to a single column, or If I should create a different table only for this section of my registration form. It's also important to take into account that I will later need to pull all this data from the database and to show it in the "admin back-end" where it would be available for edition to update the database. Below you can see part of the html code for the section containing the check-boxes.
<p>4) Please select only the product(s) you are interested in. (Anticipated purchase amounts for the quarter.)</p>
<table cellpadding="15" >
<tbody>
<tr>
<th>Handbags</th>
<th>Fashion Jewelry</th>
<th>Watches</th>
<th>Crystal Travel Jewelry</th>
<th>Fine Jewelry</th>
</tr>
<tr>
<td>
<input type="checkbox" name="pro_amount[]" value="1" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="2" />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="3" />Up to $1000 </br>
</td>
<td>
<input type="checkbox" name="pro_amount[]" value="4" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="5" />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="6" />Up to $1000 </br>
<td>
<input type="checkbox" name="pro_amount[]" value="7" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="8" />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="9" />Up to $1000 </br>
<td>
<input type="checkbox" name="pro_amount[]" value="10" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="11" />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="12" />Up to $1000 </br>
<td>
<input type="checkbox" name="pro_amount[]" value="13" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="14" />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="15" />Up to $1000 </br>
</tr>
</tbody>
</table>
<p>5) What are the average retail price points for each of the following items? (Check only those that apply.)</p>
<table cellpadding="15" >
<tbody>
<tr>
<th>Handbags</th>
<th>Jewelry</th>
<th>Watches</th>
</tr>
<tr>
<td>
<input type="checkbox" name="av_rtp[]" value="1" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="2" />$75</br>
<input type="checkbox" name="av_rtp[]" value="3" /> $40</br>
</td>
<td>
<input type="checkbox" name="av_rtp[]" value="4" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="5" />$75</br>
<input type="checkbox" name="av_rtp[]" value="6" /> $40</br>
<td>
<input type="checkbox" name="av_rtp[]" value="7" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="8" />$75</br>
<input type="checkbox" name="av_rtp[]" value="9" /> $40</br>
</tr>
</tbody>
</table>
Jsfiddle Demo if that helps.!
Thanks a lot in advance for any help you can provide.

It is an extremely poor practice to store a delimited list of data in one column. It creates querying issues (and performance issues when you have to query using a nonsargable where clause). Create a separate table.

You have a couple options…
1)Allowing one box to be checked per category:
In this case, it would be better if you changed your checkboxes to radio buttons that way it is easier to keep only one checked at one time and you will only have to store the value of the one button selected in one column of your database.
2)Allowing multiple boxes to be checked per category:
In this case, you can keep your checkboxes and would suggest storing the total value of the checkboxes in one column to keep the load on your database down. However, this means that you need to include some form of script reverse the addition you did before storing the info in your database. You could do this by having the values of the three check boxes in each category be 1, 10 and 100 so that you could easily check which ones were checked by using a series of % operations.
<table cellpadding="15" >
<tbody>
<tr>
<th>Handbags</th>
<th>Jewelry</th>
<th>Watches</th>
</tr>
<tr>
<td>
<input type="checkbox" name="av_rtp[]1" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]1" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]1" value="100" /> $40<br />
</td>
<td>
<input type="checkbox" name="av_rtp[]2" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]2" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]2" value="100" /> $40<br />
<td>
<input type="checkbox" name="av_rtp[]3" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]3" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]3" value="100" /> $40<br />
</tr>
</tbody>
</table>
*Notice how each group has a different name, to keep them separated. You should do this whether you use radio buttons or checkboxes since they mean different things.
Let me know if this helps or not.

Related

Angular ng loop without a dom element

Say I have a block of html like the following and I need to loop through an array of objects and display a label for each option.
I can do this easily using ng-repeat for the labels and the input individually, but in order for my style framework to update the checked input correctly, the input needs to be directly above the label in the document.
How can I do this without wrapping each group of input + label in another dom element? (This would destroy the style of the list as well)
need:
<input type="radio" name="rGroup" value="1" id="r1" />
<label class="radio radio-plan-lg" for="r1">
Billed every<br><span>1 month</span>
</label>
<input type="radio" name="rGroup" value="2" id="r2" />
<label class="radio radio-plan-lg" for="r2">
Billed every<br><span>3 months</span>
</label>
<input type="radio" name="rGroup" value="3" id="r3" />
<label class="radio radio-plan-lg" for="r3">
Billed every<br><span>6 months</span>
</label>
not:
<input type="radio" name="rGroup" value="1" id="r1" />
<input type="radio" name="rGroup" value="2" id="r2" />
<input type="radio" name="rGroup" value="3" id="r3" />
<label class="radio radio-plan-lg" for="r1">
Billed every<br><span>1 month</span>
</label>
<label class="radio radio-plan-lg" for="r2">
Billed every<br><span>3 months</span>
</label>
<label class="radio radio-plan-lg" for="r3">
Billed every<br><span>6 months</span>
</label>
Use ng-repeat-start:
<input ng-repeat-start="item in array" type="radio" name="rGroup" value="{{item}}" id="r{{item}}" />
<label ng-repeat-end class="radio radio-plan-lg" for="r{{item}}">
Billed every<br><span>{{item}} month</span>
</label>
See Documentation - Special repeat start and end points

Problems tick checkbox using PHPUnit (Functional tests)

I have problems to select the checkbox of the following code via PHPUnit.
<div id="user_project">
<input type="checkbox" id="user_project_1" name="user[project][]" value="1" checked="checked" />
<label for="user_project_1">Project 1</label>
<input type="checkbox" id="user_project_2" name="user[project][]" value="2" /><label for="user_project_2">Project 2</label>
<input type="checkbox" id="user_project_3" name="user[project][]" value="3" checked="checked" />
<label for="user_project_3">Project 3</label>
<input type="checkbox" id="user_project_4" name="user[project][]" value="4" checked="checked" />
<label for="user_project_4">Project 4</label>
<input type="checkbox" id="user_project_5" name="user[project][]" value="5" checked="checked" />
<label for="user_project_5">Project 5</label>
<input type="checkbox" id="user_project_6" name="user[project][]" value="6" checked="checked" />
<label for="user_project_6">Project 6</label>
<input type="checkbox" id="user_project_7" name="user[project][]" value="7" />
<label for="user_project_7">Project 7</label>
<input type="checkbox" id="user_project_10" name="user[project][]" value="10" /><label for="user_project_10">Project 10</label>
<input type="checkbox" id="user_project_12" name="user[project][]" value="12" /><label for="user_project_12">Project 12</label>
<input type="checkbox" id="user_project_13" name="user[project][]" value="13" />
<label for="user_project_13">Project 13</label>
</div>
Normally I will tick the checkboxes using "$form["Name of checkbox"]->tick();" - but i cannot change the name (because it is generated via Symfony2).
Can somebody help me?
Suppose you retrieve the form in the $form object, try this for select the 2nd element:
$select = $form->get('user[project]');
$cb = $select[1]; // the second table row
$cb->tick();
Hope this help

Adding a class to all checkboxes in cakephp (HABTM)

I have to tables associated with HABTM, and it displays the checkboxes perfectly. The line of code is this:
<?php echo $this->Form->input('Item.ItemCharacteristic',array('id'=>'item_characteristic','label' =>false,'type'=>'select','multiple'=>'checkbox','options' => $itemCharacteristics,'selected' => $this->Html->value('ItemCharacteristic.ItemCharacteristic')));
?>
what it displays is this:
<div class="input select"><input type="hidden" name="data[Item][ItemCharacteristic]" value="" id="item_characteristic"/>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="8" id="item_characteristic8" /><label for="item_characteristic8">We-Fii</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="9" id="item_characteristic9" /><label for="item_characteristic9">Test1</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="10" id="item_characteristic10" /><label for="item_characteristic10">Setting</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="11" id="item_characteristic11" /><label for="item_characteristic11">Test4</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="13" id="item_characteristic13" /><label for="item_characteristic13">Solari</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="15" id="item_characteristic15" /><label for="item_characteristic15">Panorama</label></div>
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="17" id="item_characteristic17" /><label for="item_characteristic17">BlusBrothers</label></div>
</div>
so what i want is to add a class within iput element like this:
<div class="checkbox"><input type="checkbox" name="data[Item][ItemCharacteristic][]" value="8" id="item_characteristic8" class="element" /><label for="item_characteristic8">We-Fii</label></div>
so just to add the class="element" to it..
Any ide??
You can placed your class on the field by adding 'class' => 'name' to the array
<?php echo $this->Form->input('Item.ItemCharacteristic',array('class'=>'mycustomclass' 'id'=>'item_characteristic','label' =>false,'type'=>'select','multiple'=>'checkbox','options' => $itemCharacteristics,'selected' => $this->Html->value('ItemCharacteristic.ItemCharacteristic')));
?>
I hope it solves your problem

Issue when posting empty textarea in array

I've got a dynamic form that can have n amount of rows, so the field names are an array. However I'm running into the problem that when a textarea is empty the value is not submited
This causes a discrepancy in the length of the array data (all array's should be 5 long, however only 2 notes where filled in causing the data to slip into the wrong row)
How can I post it so the resulting array for Note is ['','','','Mr Fields','No 543']?
Here's an example of three of the rows
<tr>
<td><input type="hidden" name="RateId[]" value="23"><input
type="text" name="Date[]" value="2013.08.23"
class="datepicker required hasDatepicker" id="dp1378446864655"></td>
<td>Meeting Room A
<textarea placeholder="Note" name="Note[]"
style="height: 26px;"></textarea></td>
<td class="calc"><input type="number" min="1" name="Count[]"
style="width: 3em" value="1"> x <input type="hidden"
name="Nights[]" value="1">
<div class="pull-right">
IDR <input type="number" min="1" name="Value[]" value="3000000">
</div></td>
</tr>
<tr>
<td><input type="hidden" name="RateId[]" value="22"><input
type="text" name="Date[]" value="2013.08.23"
class="datepicker required hasDatepicker valid" id="dp1378446864653"></td>
<td>Airport drop off
<textarea placeholder="Note" name="Note[]"
style="height: 26px;">Mr Fields</textarea></td>
<td class="calc"><input type="number" min="1" name="Count[]"
style="width: 3em" value="1"> x <input type="hidden"
name="Nights[]" value="1">
<div class="pull-right">
IDR <input type="number" min="1" name="Value[]" value="250000">
</div></td>
</tr>
<tr>
<td><input type="hidden" name="RateId[]" value="26"><input
type="text" name="Date[]" value="2013.08.23"
class="datepicker required hasDatepicker" id="dp1378446864654"></td>
<td>Restaurant Bill
<textarea placeholder="Note" name="Note[]"
style="height: 26px;">No 543</textarea></td>
<td class="calc"><input type="hidden" name="Count[]" value="1"><input
type="hidden" name="Nights[]" value="1">
<div class="pull-right">
IDR <input type="number" min="1" name="Value[]" value="100">
</div></td>
</tr>

use shell curl to post whith a checkbox array

The http code like this:
<input type="checkbox" value="1" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="3" name="checkbox[]">
<input type="checkbox" value="4" name="checkbox[]">
<input type="checkbox" value="5" name="checkbox[]">
<input type="checkbox" value="6" name="checkbox[]">
It will be checked some value more than one
I see that, checkbox like this is for php and have to be serialized and I want to use curl on unix shell to do this,what can i do?

Resources