$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
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
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
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
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
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
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
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
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