Skip to Content Skip to Menu

🎃 Happy Halloween! Treat yourself with an awesome discount! Get 20% off now with code SPOOKY-2024!

Check Box (Multiple) fields

  • tiekubd
  • tiekubd
  • OFFLINE
  • Posts: 68
  • Thanks: 1
  • Karma: 0
7 years 11 months ago #287858 by tiekubd
Check Box (Multiple) fields was created by tiekubd
  1. I have a Check Box (Multiple) field. The check boxes have numerical values. How do I add them up when checked in user profile?

    I have a Check Box (Multiple) field. When I check one or boxes and save, all boxes are checked . What is causing this?

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48454
  • Thanks: 8280
  • Karma: 1443
7 years 11 months ago #287870 by krileon
Replied by krileon on topic Check Box (Multiple) fields

I have a Check Box (Multiple) field. The check boxes have numerical values. How do I add them up when checked in user profile?

You have to use CB Query Field or CB Code Field to either query for them and use SQL to add them up or use PHP to add up the substitutions. Example code as follows for a Code field.

Code:
return (int) '[cb_checkbox1]' + (int) '[cb_checkbox2]' + (int) '[cb_checkbox3]';

I have a Check Box (Multiple) field. When I check one or boxes and save, all boxes are checked . What is causing this?

No idea, we have nothing that does this. You'd have to of have configured something to change the value of the field (e.g. CB Auto Actions). I am unable to confirm on 2 local installs or our demo site.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • tiekubd
  • tiekubd
  • OFFLINE
  • Posts: 68
  • Thanks: 1
  • Karma: 0
7 years 11 months ago #287879 by tiekubd
Replied by tiekubd on topic Check Box (Multiple) fields

I have a Check Box (Multiple) field. When I check one or boxes and save, all boxes are checked . What is causing this?
No idea, we have nothing that does this. You'd have to of have configured something to change the value of the field (e.g. CB Auto Actions). I am unable to confirm on 2 local installs or our demo site.


Values
Value Label
5 choice1

Value Label
5 choice2

Value Label
5 choice3

Value Label
5 choice4

Well I found something interesting out. Because I have assigned the same value (5) to my checkboxes, checking one checks all of them on save. What is that? Happening at 2 demo sites.

return (int) '[cb_checkbox1]' + (int) '[cb_checkbox2]' + (int) '[cb_checkbox3]';


Is this formula for summing the checkbox values in a single multi checkbox field? That is what I want to achieve. So in my example if choice1 and choice2 are checked for a given user, the total is 5 + 5 = 10.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48454
  • Thanks: 8280
  • Karma: 1443
7 years 11 months ago #287883 by krileon
Replied by krileon on topic Check Box (Multiple) fields

Well I found something interesting out. Because I have assigned the same value (5) to my checkboxes, checking one checks all of them on save. What is that? Happening at 2 demo sites.

You set all of their database values to be the same so of course that's going to happen. Value is used for database storage and needs to be unique. This is noted in the parameters description.

Is this formula for summing the checkbox values in a single multi checkbox field? That is what I want to achieve. So in my example if choice1 and choice2 are checked for a given user, the total is 5 + 5 = 10.

You'd need to explode the value then add the results all together. Multicheckbox and multselect fieldtypes store values with a |*| delimiter so they're stored as 5|*|6|*|7, etc.. You'd PHP explode by |*|, loop the array, and add the values. Example as follows.

Code:
$total = 0; foreach ( explode( '|*|', '[cb_myfield]' ) as $value ) { $total += (int) $value; } return $total;


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • tiekubd
  • tiekubd
  • OFFLINE
  • Posts: 68
  • Thanks: 1
  • Karma: 0
7 years 11 months ago #287887 by tiekubd
Replied by tiekubd on topic Check Box (Multiple) fields
It's good there is a way I can explode and add. :)

What is the best way of trying to do this, seeing every value has to be unique and I want to add the same values sometimes.

Do I create separate fields for what would have been separate checkboxes? I can, but it will exponentially add to my fields count.

Thanks

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48454
  • Thanks: 8280
  • Karma: 1443
7 years 11 months ago #287890 by krileon
Replied by krileon on topic Check Box (Multiple) fields
Best way is to make the values unique then strip out what makes them unique when doing the math. So for example you could have the following values

val1_5
val2_5
val3_5

That would make them unique while still allowing you to define a same points value for example. Then in your code you can use substr after a strpos, preg_match, or even and explode the extra the points value. Alternatively instead of hardcoding the value into the field value you assign a value in PHP instead which would give you the most flexibility.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum