RE: [PHP] insert loop logic
| From: | Ford, Mike [LSS] | Date: | Thu, 05 Jun 2003 11:39:37 +0000 |
| Subject: | RE: [PHP] insert loop logic | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-150324@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Ryan A [mailto:ryan@jumac.com]
> Sent: 03 June 2003 23:25
> I am getting values from a form's checkboxes
> (eg
> <input type=checkbox name='id[]' value=1>
> <input type=checkbox name='id[]' value=2>
> etc)
>
> When i get these values I want to enter them into the database but
> 1)I should enter only the first 5 (and ignore the rest) into
> the database
> 2)it has to enter 1 record for every every "id" picked
> eg:
> if id[1], id[2],id[3] and id[4] were picked it should enter this as 4
> differient records
>
>
> How do i do this? I have tried using a for loop but somewhere
> the logic is
> very very bad :-) and so i deleted the whole damn thing.
Only the values for boxes which are checked are sent, so the best way of
accessing these is with a foreach loop over the id[] array (you should also
check for no values, as then you won't even have an array at all!!). So:
if (is_array($_POST['id'])):
foreach ($_POST['id'] as $id):
// do an INSERT for the current $id
endforeach;
endif;
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211