Jump to content

Implode with Array and MySQL Result


solarisuser

Recommended Posts

I have five checkboxes that I'd like to put in an array only if they have been checked. I'm not sure exactly how to do that.

PHP Code:

[...snip...]
<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field1">Field1BR>
<INPUT TYPE=CHECKBOX VALUE="field2" NAME="field2">Field2BR>
<INPUT TYPE=CHECKBOX VALUE="field3" NAME="field3">Field3BR>
<INPUT TYPE=CHECKBOX VALUE="field4" NAME="field4">Field4BR>
<INPUT TYPE=CHECKBOX VALUE="field5" NAME="field5">Field5BR>
[...snip...]

If I have field2, field3, and field5 checked, I'd like to put them in an array, and then use implode() to print them out later.
Link to comment
https://forums.phpfreaks.com/topic/4394-implode-with-array-and-mysql-result/
Share on other sites

[!--quoteo(post=352704:date=Mar 7 2006, 08:25 PM:name=solarisuser)--][div class=\'quotetop\']QUOTE(solarisuser @ Mar 7 2006, 08:25 PM) [snapback]352704[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have five checkboxes that I'd like to put in an array only if they have been checked. I'm not sure exactly how to do that.

PHP Code:

[...snip...]
<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field1">Field1BR>
<INPUT TYPE=CHECKBOX VALUE="field2" NAME="field2">Field2BR>
<INPUT TYPE=CHECKBOX VALUE="field3" NAME="field3">Field3BR>
<INPUT TYPE=CHECKBOX VALUE="field4" NAME="field4">Field4BR>
<INPUT TYPE=CHECKBOX VALUE="field5" NAME="field5">Field5BR>
[...snip...]

If I have field2, field3, and field5 checked, I'd like to put them in an array, and then use implode() to print them out later.
[/quote]

$myarray = Array($field2, $field3, $field5);

? Or maybe this :

[code]
   $myarray = Array();
   if (isset($_REQUEST['field1'])) {
      array_push($myarray, $_REQUEST['field1']);
   }
[/code]

And just repeat the if for each field you need in there..
Only those checkboxes that have been checked are actually passed to your processing script. If you change your form to:
[code]
<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field[]">Field1BR>
<INPUT TYPE=CHECKBOX VALUE="field2" NAME="field[]">Field2BR>
<INPUT TYPE=CHECKBOX VALUE="field3" NAME="field[]">Field3BR>
<INPUT TYPE=CHECKBOX VALUE="field4" NAME="field[]">Field4BR>
<INPUT TYPE=CHECKBOX VALUE="field5" NAME="field[]">Field5BR>
[/code]

Only those checked will be in the array $_POST['field'], you can either leave them there or move them to another array. You can see what's in the array by
[code]<?php if (isset($_POST['field'])) echo '<pre>' . print_r($_POST['field']) . '</pre>'; ?>[/code]

Ken
[!--quoteo(post=352864:date=Mar 8 2006, 09:43 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 8 2006, 09:43 AM) [snapback]352864[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field[]">Field1BR>
<INPUT TYPE=CHECKBOX VALUE="field2" NAME="field[]">Field2BR>
[/code]

Only those checked will be in the array $_POST['field'], you can either leave them there or move them to
[/quote]

Oh, neat! It never even dawned on me to try that.. I use that technique with select multiple's all the time.. Thanks for the info! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.