How to retrieve a sum of array values from mysql

hi

i store the multicheckbox values as a sumofarray in database, i want to compare the values with original and set as checked , how do it can any one help me to solve the problem

for example


<input type="checkbox" value="1" name="val[]">
<input type="checkbox" value="2" name="val[]">
<input type="checkbox" value="4" name="val[]">
<input type="checkbox" value="8" name="val[]">

Your form, using POST method


<input type="checkbox" name="val[1]"> 
<input type="checkbox" name="val[2]">
// etc

The form handler:


// provided some are checked:
$vals = array_keys($_POST['val']);
echo '<hr />'. array_sum($vals) .' <hr /> ';

i have to edit these checked values how do i decode the sum values and assign the check boxes ?

i do like this but i couldnot get solution
i have assigned checkbox values as like the following


2^index0 = 1 
2^index1 = 2
2^index2 = 4
2^index3 = 8


$checkboxarray=array(1,2,4,8,16);

foreach($checkboxarray as $key => $val)
{

$db_val= 5; // sum of checkbox value check box 1,4 are selected i.e 0,2 index

				
$options_bin = base_convert ($db_val, 10, 2 ); //outputs 0101 
												
$options_arr = str_split($options_bin); //returns array containing "0,1,0,1"

for($i = 0; $i < sizeof($options_arr); $i++) {

if($i==$key && $options_arr[$i]==1 )							{

       if($i==$key && $options_arr[$i]==1 ){
            $checkboxval['checked']=$val;
      }
}

}

how do i proceed further

It looks like you’re trying to use Bitwise, but are a little unsure of how it works. Are you attempting to follow a tutorial somewhere? If so, can you link to it?


<?php
$options = array(
  1   => 'a',
  2   => 'b',
  4   => 'c',
  8   => 'd',
  16  => 'e'
);

function isChecked($key, $checked){
  return $checked & $key ? true : false ;
}

$checked = 10;

foreach($options as $key => $value){
  printf(
    '<input name="val[%d]" value="%s" %s />' . PHP_EOL,
    $key,
    $value,
    isChecked($key, $checked) ? 'checked="checked"' : ''
  );
}

/*
  <input name="val[1]" value="a"  />
  <input name="val[2]" value="b" checked="checked" />
  <input name="val[4]" value="c"  />
  <input name="val[8]" value="d" checked="checked" />
  <input name="val[16]" value="e"  />
*/

thank u for u answer

i m exactly need like this thread

http://stackoverflow.com/questions/6206955/php-mysql-insert-values-from-multiple-checkboxes-into-one-column-in-table

in this having thread having last answer i need ,i got the solution thank you

@Anthony,


<input type="checkbox"...

No?

>.<

I think a quick review of bitwise operations is in order here

What you are attempting to do is use binary, so…

0000:0001 = 1
0000:0010 = 2
0000:0100 = 4
0000:1000 = 8

and so on and so forth. the & operation (and) does this:

0001 & 0001 == true
0010 & 0010 == true
0011 & 0001 == true
0011 & 0010 == true
0010 & 0001 == false

There’s an operation called “shifts” – in php they are indicated by << and >>

A shift left by one is the same as multiply by two… but two is four, etc, etc…

0001 << 2 == 0100
0010 << 1 == 0100
0100 >> 2 == 0001

Which just quickly sums up what needs to be done here for operations.

To handle all this for a form, I’d probably make a object with the various things you need to do as methods. What ‘needs to be done’? Well, a constructor which gets passed the common ‘name’ for all the fields, the labels for all the fields, and the starting integer value you’re storing these ‘flags’ in. That’s the PROPER term for this by the way, flags/bitFlags. Each bit is a ‘flag’… from there a routine to populate from the $_POST data if need be – you flop them on validation, it helps to be able to re-send what they set so they don’t have to re-enter everything… then a routine to automatically build the labels/inputs for you, and last of all a getFlags function to pull the current state as that nice packed integer.


class checkBoxListHandler {
	private
		$prefix,
		$list=array();
	
	public function __construct($prefix,$list,$flags=0) {
		$this->prefix=$prefix;
		foreach ($list as $key => $value) {
			$bitMask=1 << $key;
			$this->list[$key]=array(
				'label' = $value,
				'checked' => $flags & $bitMask,
				'bitmask'= $bitMask
			);
		}
	} // function __construct
	
	public function populateFromForm() {
		foreach ($this->list as $key => $value) {
			/*
				we have to iterate $this->list because unchecked boxes
				often are not returned by some browsers as results!
			*/
			$this->list[$key]['checked']=(
				isset($_POST[$this->prefix][$key]) ?
				$_POST[$this->prefix][$key] :
				0
			);
		}
	} // function populateFromForm
	
	public function showForm() {
		foreach ($this->list as $key => $data) {
			echo '
				<label for="',$this->prefix,$key,'">',$data['label'],'</label>
				<input
					type="checkbox"
					id="',$this->prefix,$key,'"
					name="',$this->prefix,'[',$key,']','"
					value="',$data['bitMask'],'"',(
						$data['checked']==0 ? '' : ' checked="checked"'
					),'
				/>
				<br />';
		}
	} // function showForm
	
	public function getFlags() {
		$result=0;
		foreach ($this->list as $data) {
			$result=$result & $data['checked'];
		}
		return $result;
	} // function getFlags
	
} // class checkBoxListHandler

Untested, probably a typo or two, but you get the idea…

To create the object you’d simply pass it thus:


$myCheckboxList=new checkBoxListHandler(
	'mychecks',
	array(
		'first checkbox','second checkbox','third checkbox'
	),
	$startingPackedFlags
);

StartingPackedFlags being whatever you pull from the database, and is optional so if you’re working from the value already in your database (edit) you can handle it there… if you came from the form and rejected it, or if you are showing a new one, all you’d have to do is:


$myCheckBoxList->showForm();

and poof, there are all your labels/inputs


				<label for="mychecks_0">first checkbox</label>
				<input
					type="checkbox"
					id="mychecks0"
					name="mychecks[0]"
					value="1"
				/>
				<br />
				<label for="mychecks_1">second checkbox</label>
				<input
					type="checkbox"
					id="mychecks1"
					name="mychecks[1]"
					value="2"
				/>
				<br />
				<label for="mychecks_2">third checkbox</label>
				<input
					type="checkbox"
					id="mychecks2"
					name="mychecks[2]"
					value="4"
				/>
				<br />

When returning from a form, you just create the object and then call:


$myCheckBoxList->populateFromForm();
$checkFlags=$myCheckBoxList->getFlags();

I prefer to use the same php file to handle returns from forms as I do sending the form, so an generic ‘overview’ of such a file would probably go:


$myCheckboxList=new checkBoxListHandler(
	'mychecks',
	array(
		'first checkbox','second checkbox','third checkbox'
	),
	$startingPackedFlags
);
if isset($_POST['fromForm']) {
	$myCheckBoxList->populateFromForm();
	if (validateForm) {
		$checkFlags=$myCheckBoxList->getFlags();
		/* write it to the database here */
	} else $myCheckBoxList->showForm();
} else $myCheckBoxList->showForm();

That’s just a generic overview – you’d have a lot more going on in there, but you should get the general logic of how that object works from that.

Oh, and a little hint – assuming you’re using integer that’s typically a 32 bit variable, so you can only stuff 32 checkboxes into your one ‘flags’ variable. Also my object is ‘tied’ to the key order, so if you change the order of the array, it’ll mess up. Should you need control over the order, you can pass the array key manually thus:


$myCheckboxList=new checkBoxListHandler(
	'mychecks',
	array(
		1 => 'first checkbox',
		0 => 'second checkbox',
		5 => 'third checkbox'
	),
	$startingPackedFlags
);

Which will show them in order and populate their flags properly… using the shifts internally means you can pass any number 0…31 as your key… in any order.

A more robust version might used named keys and a counter – the ‘name’ could be used instead of prefix (or with it) and might be easier to work with server-side than numbers.

Oh, @anthony – you know printF is slow as molasses, and your isChecked function adds pointless overhead to the code right? (two conditionals and a function call for what should be a single conditional?!?). I mean, I could see it if multiple operations were being performed, but a single binary “and”?!?

i have to show the selected checkbox , my database value is 5, i have select the checkboxes are first and third, how to do can you give example.

thanks you for all who answered this post

Thank you for your example the above example works fine to me.