CodeIgniter Forums
How to insert the values from each checked checkboxes into the database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to insert the values from each checked checkboxes into the database (/showthread.php?tid=55120)

Pages: 1 2


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]towki[/eluser]
Lets consider this example:
When inserting a new crime report, it could have one or many offenses or crime committed and those crimes are listed and each can be selected by a checkbox.

So how could i insert the individual values from each checked or selected checkboxes into my database table?

Please give me sample codes, i'll try to understand.


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]LuckyFella73[/eluser]
Would be a bit easier to use radio buttons ("yes"/"no") for you could
driectly save the value into your database. When using
checkboxes you only get a post value for a checkbox if it's
checked. That way you would need multiple if conditions to save
a meaningful value into database.

The user guide provides enough code exmples covering what you want to do btw.
Please try first and come back if you get stuck.


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]xeroblast[/eluser]
in the database, create table tablename (checkbox_field TEXT);.
in your code, all checkboxes must have same name with a bracket "checkboxname[]" and with different values
after the post request, loop through the "checkboxname[]"
Code:
foreach ( $this->input->post('checkboxname') as checkbox ) {
if ($checkboxvalue == '') $checkboxvalue .= checkbox;
else $checkboxvalue .= '-+-'.checkbox; // '-+-' is your delimiter
}
save or insert the $checkboxvalue to your database field.
in getting or displaying the values, use php explode $displaycheckboxvalues = explode('-+-',$checkboxvaluesfromdatabase->checkbox_field); // 1st parameter is your delimiter
Code:
foreach ($displaycheckboxvalues as $v) echo $v;



How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]towki[/eluser]
[quote author="LuckyFella73" date="1349947809"]Would be a bit easier to use radio buttons ("yes"/"no") for you could
driectly save the value into your database. When using
checkboxes you only get a post value for a checkbox if it's
checked. That way you would need multiple if conditions to save
a meaningful value into database.

The user guide provides enough code exmples covering what you want to do btw.
Please try first and come back if you get stuck.[/quote]

thanks for the reply but i dont think of using radio buttons since i'll be selecting multiple values.


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]LuckyFella73[/eluser]
Quote:thanks for the reply but i dont think of using radio buttons since i’ll be selecting multiple values.

What I meant was 2 radio buttons ("yes" and "no") for each (instead of) checkbox.
It's just because I don't like to do if statements is not nessecary Wink And I like
to have meaningful values in my database.

I don't know how many checkboxes you are going to set up. If you have maaaany and
you do it looping through a checkbox array it's a bit more tricky when it comes to
saving the values into database - get the right value into the related column.

Using the radio button solution the names of the radio buttons would (should) match the
column names in db - that way it's easier when it comes to save the values.


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]towki[/eluser]
[quote author="LuckyFella73" date="1349970626"]
Quote:thanks for the reply but i dont think of using radio buttons since i’ll be selecting multiple values.

What I meant was 2 radio buttons ("yes" and "no") for each (instead of) checkbox.
It's just because I don't like to do if statements is not nessecary Wink And I like
to have meaningful values in my database.

I don't know how many checkboxes you are going to set up. If you have maaaany and
you do it looping through a checkbox array it's a bit more tricky when it comes to
saving the values into database - get the right value into the related column.

Using the radio button solution the names of the radio buttons would (should) match the
column names in db - that way it's easier when it comes to save the values.[/quote]

I got the idea now, but checkboxes I think would be the most appropriate. There are I think at least 20 values ( which im referring to the name of crimes or offenses committed). Wouldnt be nice to just check the type of crimes committed? If only i could use radio buttons, I think it would be more easy.





How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]towki[/eluser]
[quote author="xeroblast" date="1349954632"]in the database, create table tablename (checkbox_field TEXT);.
in your code, all checkboxes must have same name with a bracket "checkboxname[]" and with different values
after the post request, loop through the "checkboxname[]"
Code:
foreach ( $this->input->post('checkboxname') as checkbox ) {
if ($checkboxvalue == '') $checkboxvalue .= checkbox;
else $checkboxvalue .= '-+-'.checkbox; // '-+-' is your delimiter
}
save or insert the $checkboxvalue to your database field.
in getting or displaying the values, use php explode $displaycheckboxvalues = explode('-+-',$checkboxvaluesfromdatabase->checkbox_field); // 1st parameter is your delimiter
Code:
foreach ($displaycheckboxvalues as $v) echo $v;
[/quote]

thanks for the reply. I will try to learn and apply your example as soon I log in to my pc. Please check back here often, i might have some questions.

BTW anyone can still reply to this topic. Anymore help will be appreciated.


How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]CroNiX[/eluser]
I doubt that code will work because of the $checkboxvalue == '' (it never will be an empty string, it will have the checkbox value or boolean FALSE if it wasn't sent [not checked])





How to insert the values from each checked checkboxes into the database - El Forum - 10-11-2012

[eluser]xeroblast[/eluser]
[quote author="CroNiX" date="1349977570"]I doubt that code will work because of the $checkboxvalue == '' (it never will be an empty string, it will have the checkbox value or boolean FALSE if it wasn't sent [not checked])
[/quote]
thanks for the correction CroNix, i just written what comes first in my mind. hehehe...
Code:
$first = true;
foreach ( $this->input->post('checkboxname') as checkbox ) {
if ($first) {
  $checkboxvalue .= checkbox;
  first = false;
} else {
  $checkboxvalue .= '-+-'.checkbox; // '-+-' is your delimiter
}
}
//----------- OR --------------//
foreach ( $this->input->post('checkboxname') as checkbox ) {
$checkboxvalue .= checkbox.'-+-'; // '-+-' is your delimiter
}
process_to_remove_the_last_delimiter( $checkboxvalue );
uncheck checkbox will not be sent to the process form only the check ones..


How to insert the values from each checked checkboxes into the database - El Forum - 10-15-2012

[eluser]towki[/eluser]
[quote author="xeroblast" date="1350012736"]
Code:
$first = true;
foreach ( $this->input->post('checkboxname') as checkbox ) {
if ($first) {
  $checkboxvalue .= checkbox;
  first = false;
} else {
  $checkboxvalue .= '-+-'.checkbox; // '-+-' is your delimiter
}
}
//----------- OR --------------//
foreach ( $this->input->post('checkboxname') as checkbox ) {
$checkboxvalue .= checkbox.'-+-'; // '-+-' is your delimiter
}
process_to_remove_the_last_delimiter( $checkboxvalue );
uncheck checkbox will not be sent to the process form only the check ones..[/quote]

Could you please explain to me the logic of your code. That delimiter thing also confuses me. I still could not apply your code, since i think there are modifications to be made in your example code. Whaa im sorry for such a newbie question and for my english also :-D