Jump to content

checkboxes + array + !selected


jamkelvl

Recommended Posts

Okay, so have a list of checkboxes that store in a serialized array and then to database.

 

I'm wondering, if at all possible, how to store a value in the array if one or more checkboxes have not been checked.

 

Basically, if one checkboxe is checked it stores in the array fine, but as for the rest of the uncheckedboxes I want to store a value in the array such as x for each uncheckedbox.

 

Here is my code thus far

 

<?php
								if (in_array($initials, $_POST['assignedTo'])) { 
										// do nothing
									} else {
										echo '<p>'.$name.'<input type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" style="margin-right: 75%; float: right;" /><br/>';
										echo '<input type="text" name="assignedTo[]" id="'.$name.'" value="0" onFocus="startCalc();" onBlur="stopCalc();" /></p>';
									}
									foreach ($_POST['assignedTo'] as $value) {
										if ($value == $initials) {
											echo '<p>'.$name.'<input type="checkbox" name="assignedTo[]" id="assignedTo" style="margin-right: 75%; float: right;" value="'.$initials.'" checked />';						
											echo '<input type="text" name="assignedTo[]" id="'.$name.'" value="0" onFocus="startCalc();" onBlur="stopCalc();" /></p>';
										}
									}
?>

Link to comment
https://forums.phpfreaks.com/topic/190662-checkboxes-array-selected/
Share on other sites

Hmm.. yeah I ended up coming up with a bandaid.

 

Basically, before inserting into database, I loop thru the array and add a new value 'x' before each value... errraa something along those lines.

 

Either way.. it works...for now.

 

<?php
					$newArray = array();

						//padd x's into array
						foreach ($_POST['assignedTo'] as $value) {
							// if initials, hour value MUST be set
							// puts initial, iterates again and puts value, if its 0 this will MESSSS UPPP as it adds an x
							if ($value == '0') {
								$newArray[] = 'x';
								$newArray[] = $value;
							} else {
								$newArray[] = $value;
							}
						}

						$sNewArray = serialize($newArray);
?>

Not a bandaid... that's sort of what I was suggestioning anyways. Part science, part art, and part trying to make a square block fit into the circle hole... round the edges until it fits... then it fits... not efficient but if you don't have time to cut a new block like you should have to begin with to fit  then trim the one you have. I love programming inefficiently makes me feel warm inside. haha... but it also makes your head hurt when you have to deal with it later...

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.