Jump to content


Photo

Implode with Array and MySQL Result


  • Please log in to reply
3 replies to this topic

#1 solarisuser

solarisuser
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 08 March 2006 - 01:25 AM

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.

#2 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 02:00 PM

[!--quoteo(post=352704:date=Mar 7 2006, 08:25 PM:name=solarisuser)--][div class=\'quotetop\']QUOTE(solarisuser @ Mar 7 2006, 08:25 PM) View Post[/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 :

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

And just repeat the if for each field you need in there..
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 March 2006 - 02:43 PM

Only those checkboxes that have been checked are actually passed to your processing script. If you change your form to:
<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>

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
<?php if (isset($_POST['field'])) echo '<pre>' . print_r($_POST['field']) . '</pre>'; ?>

Ken

#4 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 05:38 PM

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

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! :)
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users