0

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.

7
Contributors
20
Replies
21
Views
7 Years
Discussion Span
Last Post by rm_daniweb
0
$tmp =$_REQUEST["check1"];
$tmp .= ",". $_REQUEST["check2"];
$tmp .= ",". $_REQUEST["check3"];

BTW, please use code tags.

0

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

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

Comments
nice suggestion
0

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

0
echo RemoveComma($checkstring);

or just use.

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

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

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

0

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

try it please

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

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

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.

0

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

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.

0

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

0

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

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.

0

dont underestimate the others.... :(

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

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.