I have a desktop apps with mysql database (can not be Change) which i want to create a web interface with php. in this apps a form contain two checkbox can be select both and a list/option box. like this below for eg.
<p>
chk value 4-
<input name="chk1" type="checkbox" id="chk1" value="4" />
<label for="sel"></label>
</p>
<p>
chk value 1024-
<input name="chk2" type="checkbox" id="chk2" value="1024" />
<label for="chk2"></label>
</p>
SELECT <select name="sel" id="sel">
<option value="0">None</option>
<option value="8">option-1 val 8</option>
<option value="16">option-2 val 16</option>
<option value="8192">option-3 val 8192</option>
<option value="16384">option-4 val 16384</option>
</select>
and in MySQL single value stored with SUM of 3 input, like if both checkbox selected and option 1 selected.. value stored 1036 (chk1 value=4 + chk2 value=1024 + option1 value=8).
What i want is a formula to reverse this MySQL value to get the actual element values for echo.
So far only working way i have in my head is with if condition
if($value == '4'){
chk1 will be selected | option none
}else if($value == '1024'){
chk2 will be selected | option none
}else if($value == '8'){
option 1.....
}else if($value == '16'){
option 2
}else if($value == '8192'){
option 3
}else if($value == '16384'){
option 4
}else if($value == '1028'){
chk1 and option 1 will be selected
}else if($value == '1032'){
chk1, chk2 and option 1 will be selected
}.... and on
is there any formula to get it done except if condition? Thanks in advance.
</div>
Assuming the form is POSTED, you could:
$checkboxes = [ 'chk1', 'chk2' ]; // for convenience since unchecked boxes don't appear in the POST array
$sum = 0;
foreach ($checkboxes as $c) {
if (isset($_POST[$c)) {
$sum += (int)$_POST[$c];
}
}
$sum += (int)$_POST['sel'];
To set the values in the form you could use something like this:
function getBits($sum) {
$bit = 1;
$vals = [];
for (; $bit < $sum; $bit <<=1) {
if ($sum & $bit) {
$vals[] = $bit;
}
}
return $vals;
}
$vals = getBits($sum);
For the select, you could remove the known checkbox values from the array and if anything remains, its the select value:
$nonCheckBox = array_filter($vals, function($v) { return $v != 4 && $v != 1024; });
$selected = count($nonCheckBox) ? $nonCheckBox[0] : 0;
Then in your form you could the $vals array:
<input type="checkbox" name="chk1" value="4" <?php echo in_array(4,$vals) ? 'CHECKED' : '' ?> >
and $selected for the select...
<option value="0" <?php echo $selected==0 ? 'selected' : '' ?>>None</option>
<option value="8" <?php echo $selected==8 ? 'selected' : '' ?> >option-1 val 8</option>
If loading from the server, I prefer to put the options in an array and generate the options in a loop to set the select. So in php I would have:
$options=[
0 => 'None',
8 => 'Option-1 Val 8',
//... etc
];
and the in my rendering:
<select ...>
<?php foreach ($options as $k=>$v) : ?>
<option value="<?php echo $k ?>" <?php echo $selected==$k ? 'selected' : '' ?>><?php echo $v ?></option>
<?php endforeach; ?>
</select>
You can store the all selected values in the serialize format in a single column by this way .
Suppose your selected values
$check1 = $_POST['chk1']??$_POST['chk1'];
$check2 = $_POST['chk2']??$_POST['chk2'];
$sel = $_POST['sel']??$_POST['sel'];
$array = array($check1,$check2,$sel);
$data = serialize($array);
And when you will get the serialize data from the Database then unserialise the that column data via this way
$selected_data = unserialize($serialize_data);
And now want to get the selected values sum .Then you can use the array_sum()
function
$selected_values_sum = array_sum($selected_data);
/// Iterate stored values as required
foreach($selected_data as $value){
}