2

I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.

My View:

<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>

And my Model looks like this:

...

elseif($checked_my_group == 1) {

                    foreach ( ?? ) {
                    $new_mail = array(
                    'to_user'           =>  $this->input->post('user_my_group_msg'),
                    );

                    $this->db->insert('mailbox', $new_mail);
                }

            }
...rest code....

Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?

3
  • 1
    this name name="user_my_group_msg" should be name="user_my_group_msg[]" Commented Dec 29, 2015 at 11:13
  • Yes but what I must use in foreach() ? I dont have idea :// Commented Dec 29, 2015 at 11:26
  • name="user_my_group_msg" should be name="user_my_group_msg[]" and foreach $this->input->post['user_my_group_msg'] Commented Dec 29, 2015 at 11:29

2 Answers 2

2

simple thing is use serialize function don't use foreach

serialize $this->input->post('user_my_group_msg')
Sign up to request clarification or add additional context in comments.

2 Comments

Doesnt work :( If I insert to database using this method I get one row in database but inside column "to_user" i have strange datas :( I must INSERT one ROW as ONE checkbox :( E.x. Checkbox1 - checked Checkbox2 - checked Checkbox3 - no checked But in database should be as: row1: ------ datas from checkbox1 row2: ------ datas from checkbox2 row3: Didn't create becouse I dont click "check" ... Sorry for my bad English :(
then you need to change checkbox name.Each checkbox have different name and it would be easy to save and update
0

try it once it will help you your View code will look as like

<?php foreach($myKidsGroupID as $row): ?>
    <label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
    <?php endforeach; ?>

Model will be corrected like this

<?php  
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {

    foreach ( $this->input->post('user_my_group_msg') as $value ) {
        $new_mail = array(
        'to_user'           =>  $value,
        );
        $this->db->insert('mailbox', $new_mail);
    }

}
.....rest code
        ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.