且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数据库中存储多个复选框和radiobutton值?

更新时间:2023-01-22 11:36:16

如果你想在一个列中存储多个复选框值,那么你应该将复选框的集合转换为二进制数



例如。

CHKBOX1 = 0

CHKBOX2 = 1

CHKBOX4 = 1

CHKBOX8 = 1



存储在Db中的数字将是14(即1 * 8 + 1 * 4 + 1 * 2 + 0 * 1)



要设置数据库中的值,您需要反转该过程 - 即取数字14

获取二进制表示并相应地设置复选框。
If you want to store multiple checkbox values in a single column then you should turn the collection of checkboxes into a binary number

eg.
CHKBOX1 = 0
CHKBOX2 = 1
CHKBOX4 = 1
CHKBOX8 = 1

The number to store in the Db would then be 14 (i.e. 1*8 + 1*4 + 1*2 + 0*1)

To set the values from the database you need to reverse the process - i.e. take the number 14
get the binary representation and set the checkboxes appropriately.