Reputation: 1313
I have a page with around 50 input check boxes, of which their
name="form[]"
I need my php script to be able to loop through all the check boxes and which ever ones are checked and submitted I need to put into a variable so I can store which forms where given.
This is my loop :
if ($this->input->post('action') == 'additional') { // Checks if radio button was checked
foreach ($this->input->post('form') as $forms) { // If above ^^^ is true loop through form[]
$givenforms = ', '.$forms.''; // What ever form[] is in the array assign them to the givenforms variable
}
$comments = 'This student was given'.$givenforms.''; // The comment I want to store in the database
}
This works but for only one form (check box). If for some reason my client needs to give a student all 50 forms I want the $comment = 'This student was given ...................................................(all 50 forms)'
Any links or a tip would be much appreciated.
Upvotes: 1
Views: 164
Reputation: 1479
$givenforms = ', '.$forms.'';
is wrong, beacause each run through the loop will overwrite the previous.
Use .=
(concatenation operator) instead of =
.
Also make sure to set the variable by using $givenforms = "";
outside the loop before concatenating it using $givenforms .= ...........
If you don't do this you will get a warning (or notice, not sure).
Upvotes: 1
Reputation: 70139
You're overwriting the value in each iteration with =
instead of concatenating .=
, but I believe you can use implode
for your use case:
if ($this->input->post('action') == 'additional') { // Checks if radio button was checked
$givenforms = implode(', ', $this->input->post('form'));
$comments = 'This student was given'.$givenforms;
}
Upvotes: 4