943,685 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1369
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 2nd, 2009
0

checkbox

Expand Post »
hi
i want to insert a symbol between all the chek box value
eg:the value will be either 1 or 0.
php Syntax (Toggle Plain Text)
  1. $tmp =$_REQUEST["check1"];
  2. $tmp .=$_REQUEST["check2"];
  3. $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.
Last edited by peter_budo; Apr 5th, 2009 at 5:22 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 9
Solved Threads: 4
Junior Poster
queenc is offline Offline
145 posts
since Mar 2008
Apr 2nd, 2009
0

Re: checkbox

php Syntax (Toggle Plain Text)
  1. $tmp =$_REQUEST["check1"];
  2. $tmp .= ",". $_REQUEST["check2"];
  3. $tmp .= ",". $_REQUEST["check3"];

BTW, please use code tags.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Apr 2nd, 2009
0

Re: checkbox

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
Reputation Points: 9
Solved Threads: 4
Junior Poster
queenc is offline Offline
145 posts
since Mar 2008
Apr 2nd, 2009
1

Re: checkbox

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 Syntax (Toggle Plain Text)
  1. <?php
  2. if(isset($_REQUEST["submit"])){
  3. $checkarray = array();
  4. $numCheckBoxes = 5; //number of checkboxes to evaluate
  5. for($i=1; $i<=$numCheckBoxes;$i++){
  6. if(empty($_REQUEST["check".$i])){
  7. array_push($checkarray,0);
  8. }else{
  9. array_push($checkarray,1);
  10. }
  11. }
  12. $checkstring = implode(",",$checkarray);//create comma separated string from array
  13. echo $checkstring;//outputs:1,0,1,1,0
  14. }
  15. ?>
This code assumes that the checkboxes are named check1,check2,check3,check4,check5...
Last edited by buddylee17; Apr 2nd, 2009 at 11:22 am.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Apr 7th, 2009
0

Re: checkbox

ya thanks its working.can u please tell me how to insert them. i want to discard the ,(commas) and insert the variables
Reputation Points: 9
Solved Threads: 4
Junior Poster
queenc is offline Offline
145 posts
since Mar 2008
Apr 7th, 2009
0

Re: checkbox

echo RemoveComma($checkstring);

or just use.

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

function RemoveComma($a) {
		 $str = str_replace(',', '', $a); 
		 return $str;
		 }
Last edited by rm_daniweb; Apr 7th, 2009 at 4:21 am.
Reputation Points: 13
Solved Threads: 12
Junior Poster
rm_daniweb is offline Offline
165 posts
since Jan 2007
Apr 7th, 2009
0

Re: checkbox

i want them to be stored in a array.should split
Reputation Points: 9
Solved Threads: 4
Junior Poster
queenc is offline Offline
145 posts
since Mar 2008
Apr 7th, 2009
0

Re: checkbox

yes you can stored in a array easily. what do u mean by "should split" text
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 7th, 2009
0

Re: checkbox

i want them to be stored in a array
a[0]=1;
a[1]=1;
a[2]=1;
something like this
Reputation Points: 9
Solved Threads: 4
Junior Poster
queenc is offline Offline
145 posts
since Mar 2008
Apr 7th, 2009
0

Re: checkbox

The way I would do for performance and speed is as follows:
php Syntax (Toggle Plain Text)
  1. $tmp =$_REQUEST["check1"];
  2. $tmp .=',' . $_REQUEST["check2"];
  3. $tmp .=',' . $_REQUEST["check3"];
  4. $tmp = str_replace(',,',',',$tmp);
  5.  
  6. //now to turn into an array
  7. $darray = explode(',',$tmp);
  8.  
  9. //now to display the array
  10. echo '<xmp>';
  11. print_r($darray);
  12. echo '</xmp>';
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Drop down box problem
Next Thread in PHP Forum Timeline: Passing GET variable of previous page to present page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC