Handling Multiple check boxes

This is a discussion on Handling Multiple check boxes within the PHP Language forums, part of the PHP Programming Forums category; On Thu, 29 Apr 2004 22:14:59 +0200 Jan Pieter Kunst <devnull@cauce.org> wrote: > In ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 04-29-2004
Kelly Thompson
 
Posts: n/a
Default Re: Handling Multiple check boxes

On Thu, 29 Apr 2004 22:14:59 +0200
Jan Pieter Kunst <devnull@cauce.org> wrote:

> In article <Y0dkc.41886$OU.978179@news20.bellglobal.com>,
> "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
>
> > > Make the "name" attribute an array.
> > >
> > > <input type="checkbox" name="checkbox_id[1]" value="a" />
> > > <input type="checkbox" name="checkbox_id[3]" value="b" />
> > > <input type="checkbox" name="checkbox_id[56]" value="c" />
> > > <input type="checkbox" name="checkbox_id[145]" value="d" />
> > >
> > > If a and c are checked, then after submitting
> > > $_REQUEST['checkbox_id'] is an array containing this: [1] => a
> > > [56]=> d.

>
> (Should be [56] => c, sorry)
>
> > How can I loop through it without knowing how many elements it
> > holds?

>
> That's what foreach() is for! See
> <http://nl.php.net/manual/en/control-structures.foreach.php>.
>
> Assuming that the form was submitted with method="post":
>
> foreach($_POST[checkbox_id] as $record_id => $value) {
>
> do_something_in_database($record_id);
> ...
> }


You can also do

reset($array);

while( list($key,$val) = each( $array ) ) {

echo $key . ' => ' . $val;

}


Reply With Quote
  #12 (permalink)  
Old 04-29-2004
Adrian Parker
 
Posts: n/a
Default Re: Handling Multiple check boxes


"Jan Pieter Kunst" <devnull@cauce.org> wrote in message
news:devnull-0F9CAE.22343729042004@news1.news.xs4all.nl...
> In article <qkdkc.41916$OU.979436@news20.bellglobal.com>,
> "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
>
> > This seems to work:
> >
> > foreach(array_keys($_POST["checkBox_Delete"]) as $key)
> > {
> > if ($deleteCount == 0)
> > {
> > $deleteThese = "'" . $key . "'";
> > $deleteCount += 1;
> > } else {
> > $deleteThese .= ", '" . $key . "'";
> > }
> > }
> >
> >
> > This look good to you?

>
> If you need the number of checked checkboxes, it is faster to get that
> like this:
>
> $deleteCount = count($_POST['checkBox_Delete']);


Nah, the deleteCount is just used to see if it's the first element to be
added to the string deleteCount.

deleteCount is building a single quote and comma delimited string that will
be used as argument for a mySQL IN function.



Adrian


Reply With Quote
  #13 (permalink)  
Old 04-30-2004
Jan Pieter Kunst
 
Posts: n/a
Default Re: Handling Multiple check boxes

In article <1Cekc.42137$OU.992778@news20.bellglobal.com>,
"Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:

> Nah, the deleteCount is just used to see if it's the first element to be
> added to the string deleteCount.
>
> deleteCount is building a single quote and comma delimited string that will
> be used as argument for a mySQL IN function.


Ah, I see. In that case this will build the querystring in less code:

$querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
array_keys($_POST['checkbox_delete']) . "')";

JP

--
Sorry, <devnull@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Reply With Quote
  #14 (permalink)  
Old 04-30-2004
Adrian Parker
 
Posts: n/a
Default Re: Handling Multiple check boxes


"Jan Pieter Kunst" <devnull@cauce.org> wrote in message
news:devnull-D4BAD2.00192430042004@news1.news.xs4all.nl...
> In article <1Cekc.42137$OU.992778@news20.bellglobal.com>,
> "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
>
> > Nah, the deleteCount is just used to see if it's the first element to be
> > added to the string deleteCount.
> >
> > deleteCount is building a single quote and comma delimited string that

will
> > be used as argument for a mySQL IN function.

>
> Ah, I see. In that case this will build the querystring in less code:
>
> $querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
> array_keys($_POST['checkbox_delete']) . "')";


Cool, thanks.


Adrian


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0