简体   繁体   中英

Save multiple same named field to different records

I have an issue where I'm trying to save a field with the same name to new separate records.

It seems to only save the last inputted field ( the second input ) to a new record, NOT the first inputted field to a new record.

I require the two inputted fields to save as new separate records

View Code -

<div class="row">
<div class="col-xs-12">
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="fa fa-barcode"></i>Oven 1</div>
            <div class="tools">
                <a href class="collapse"></a>
            </div>
        </div>
        <div class="portlet-body form">
            <div class="form-body">
                <div class="row">                   
                    <div class="col-xs-12 col-sm-3">
                        <div class="form-group">
                        <?php echo Form::label('height', 'height', array('class' => 'col-md-3 control-label')) ?>
                            <div class="col-md-9">
                                <?php echo Form::input('height[1]', $check->height, array('id' => 'height', 'class' => 'form-control')) ?>
                            </div>
                        </div>
                    </div>                                                 
                </div>                     
            </div>
        </div>           

        <div class="portlet-body form">
            <div class="form-body">
                <div class="row">                   
                    <div class="col-xs-12 col-sm-3">
                        <div class="form-group">
                        <?php echo Form::label('height', 'height', array('class' => 'col-md-3 control-label')) ?>
                            <div class="col-md-9">
                                <?php echo Form::input('height[2]', $check->height, array('id' => 'height', 'class' => 'form-control')) ?>
                            </div>
                        </div>
                    </div>                                                 
                </div>                     
            </div>
        </div>
    </div>
</div>

Controller Code -

    public function action_clean()
{
    $id = $this->request->param('id');

    if ($_POST) 
    {             
        $check = $cat->height->find();

        foreach($_POST['height'] as $key=>$value){ 

            $check->height = $value;           
        }

        $check->dt_logged = time();                
        $check->user_id = Auth::instance()->get_user()->id;                
        $check->cat = $cat->id;          
        $check->save();

        echo json_encode(array('id' => $cat->id));            
    }

}

To recap I require the two inputted fields to save as new separate records

Any help would be greatly appreciated.

Cheers

I think this is because you just overwrite your height variable in the loop, and the last value will remain in it, and not save it in the loop. Try:

 foreach($_POST['height'] as $key=>$value){ 

    $check->height = $value;
    $check->dt_logged = time();                
    $check->user_id = Auth::instance()->get_user()->id;                
    $check->cat = $cat->id;          
    $check->save();         
}

Or, if you want everything to be the same, except the height then:

    $check->dt_logged = time();                
    $check->user_id = Auth::instance()->get_user()->id;                
    $check->cat = $cat->id;
    foreach($_POST['height'] as $key=>$value){ 


        $check->height = $value;
        $check->save();         
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM