954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

checkbox

hi
i want to insert a symbol between all the chek box value
eg:the value will be either 1 or 0.

$tmp =$_REQUEST["check1"];
$tmp .=$_REQUEST["check2"];
$tmp .=$_REQUEST["check3"];


this how i have requested the value .in order to separate the values .i want to add some symbols(like comma)plz tell me how to do that..

example
1,0,1,0,0
the above output i am trying to attain.

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 
$tmp =$_REQUEST["check1"];
$tmp .= ",". $_REQUEST["check2"];
$tmp .= ",". $_REQUEST["check3"];


BTW, please use code tags.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

if the user is not selecting all the check boxes .the out put is like this.
1,,,1,,,1,,,1,1,,1,1,,1,1,
i want it to be like if the user select it should contain the value.otherwise "0" should be placed
like 0,0,1,1,1

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 

Here's a possible solution.
Step 1, Create an empty array.
Step 2, Evaluate each checkbox in a loop. If empty, push a 0 into the array. Else, push a 1 into the array.
Step 3, Implode the array using a comma as the "string glue":

<?php
if(isset($_REQUEST["submit"])){
$checkarray = array();
$numCheckBoxes = 5; //number of checkboxes to evaluate
	for($i=1; $i<=$numCheckBoxes;$i++){	
		if(empty($_REQUEST["check".$i])){
		array_push($checkarray,0);
		}else{
		array_push($checkarray,1);	
		}
	}
$checkstring = implode(",",$checkarray);//create comma separated string from array
echo $checkstring;//outputs:1,0,1,1,0 
}
?>

This code assumes that the checkboxes are named check1,check2,check3,check4,check5...

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

ya thanks its working.can u please tell me how to insert them. i want to discard the ,(commas) and insert the variables

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 


echo RemoveComma($checkstring);

or just use.

$str = str_replace(',', '', $a);

function RemoveComma($a) {
$str = str_replace(',', '', $a);
return $str;
}

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

i want them to be stored in a array.should split

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 

yes you can stored in a array easily. what do u mean by "should split" text

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

i want them to be stored in a array
a[0]=1;
a[1]=1;
a[2]=1;
something like this

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 

The way I would do for performance and speed is as follows:

$tmp =$_REQUEST["check1"];
$tmp .=',' . $_REQUEST["check2"];
$tmp .=',' . $_REQUEST["check3"];
$tmp = str_replace(',,',',',$tmp);

//now to turn into an array
$darray = explode(',',$tmp);

//now to display the array
echo '<xmp>';
print_r($darray);
echo '</xmp>';
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

try it please

<?
$myarray="";
$myarray=explode(',',$checkstring);
echo "my array".$myarray;
?>
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

it is printing as array

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 
it is printing as array


That is because the example BzzBee posted isn't very good. Try mine and just as a reminder is as follows:

$tmp =$_REQUEST["check1"];
$tmp .=',' . $_REQUEST["check2"];
$tmp .=',' . $_REQUEST["check3"];
$tmp = str_replace(',,',',',$tmp);
 
//now to turn into an array
$darray = explode(',',$tmp);
 
//now to display the array
echo '<xmp>';
print_r($darray);
echo '</xmp>';
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

That is because the example BzzBee posted isn't very good. Try mine and just as a reminder is as follows:

$tmp =$_REQUEST["check1"];
$tmp .=',' . $_REQUEST["check2"];
$tmp .=',' . $_REQUEST["check3"];
$tmp = str_replace(',,',',',$tmp);
 
//now to turn into an array
$darray = explode(',',$tmp);
 
//now to display the array
echo '<xmp>';
print_r($darray);
echo '</xmp>';

dont be so over.its not the matter of good or bad. i just told her how to declare an array. i didnt display the elements of array as her/his question was how i can make an array.
you also did the same, but just print the array as well.

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

that code is just of declaring an array and you can print just to apply print command.

<?
$myarray="";
$myarray=explode(',',$checkstring);
echo "my array".$myarray; // your array
?>


now you can see the elements of array.

<?
$myarray="";
$myarray=explode(',',$checkstring);
print_r($myarray); // array elements
?>
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 
now you can see the elements of array.
<? $myarray=""; $myarray=explode(',',$checkstring); print_r($myarray); ?>


Still buggy.
Try replacing your second line with $myarray='1,0,1,0,0,1,1,0';
Also you have an invalid input into the explode function as the variable $checkstring hasn't been set.
So your example should be the following:

<?
$myarray='1,0,1,0,0,1,1,0';
$myarray=explode(',',$myarray);
echo '<xmp>';
print_r($myarray);
echo '</xmp>';
?>

That should fix all your bugs.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Still buggy. Try replacing your second line with $myarray='1,0,1,0,0,1,1,0'; Also you have an invalid input into the explode function as the variable $checkstring hasn't been set. So your example should be the following:

<?
$myarray='1,0,1,0,0,1,1,0';
$myarray=explode(',',$myarray);
echo '<xmp>';
print_r($myarray);
echo '</xmp>';
?>

That should fix all your bugs.


ooo hello... i am doing with with reference of previous code

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

that is complete code. the green portion was already done so i did the next task.

<?php
if(isset($_REQUEST["submit"])){
$checkarray = array();
$numCheckBoxes = 5; //number of checkboxes to evaluate
for($i=1; $i<=$numCheckBoxes;$i++){
 if(empty($_REQUEST["check".$i])){
array_push($checkarray,0);
}else{
 array_push($checkarray,1);
      }
      $checkstring = implode(",",$checkarray);//create comma separated string from array

      echo $checkstring;//outputs:1,0,1,1,0

      }
$myarray="";
$myarray=explode(',',$checkstring);
print_r($myarray);
?>
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

dont underestimate the others.... :(

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

why not use an array name as the inputs name.

<input type="checkbox" name="check[]" value="1" />


then the results will already be in an array in the $_POST array. all you do after that is validate.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You