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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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; } |
|
|||
|
"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 |
|
|||
|
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" = @. |
|
|||
|
"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 |