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.

Recommended Answers

All 20 Replies

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

BTW, please use code tags.

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

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...

commented: nice suggestion +4

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

echo RemoveComma($checkstring);

or just use.

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

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

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

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

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

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>';

try it please

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

it is printing as array

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>';

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.

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
?>

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.

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

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);
?>

dont underestimate the others.... :(

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.

dont underestimate the others.... :(

REMEMBER GUYS! "There is always a greater person than you are (>U) ".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.