dos71253 2017-01-31 13:50
浏览 17
已采纳

如何在DB中将多个复选框保存为整数

I have this in my view:

enter image description here

And i have only one field in my DB and that is driverslicensetype

how to save this to the DB so that whatever types user chooses will save the picked ones like this:

        /* ABCD
         *
         * A: 0 No 1 AM 2 A1 3 A2 4 A;
         * B: 0 No 1 B1 2 B 3 BE
         * C: 0 No 1 C1 2 C1E 3 C 4 CE
         * D: 0 No 1 D1 2 D1E 3 D 4 DE
         */

so if user chooses A and B only the saved number should be 3300

A B and C would be 3230

A B C D would be 3233

B C1 would be 0210

and so on, i hope you understand.

So i have made this in my view:

<div class="col">
    <?php
    $driverslicense_types = [
        'AM',
        'A1',
        'A2',
        'A',
        'B1',
        'B',
        'BE',
        'C1',
        'C1E',
        'C',
        'CE',
        'D1',
        'D1E',
        'D',
        'DE'
    ]
    ?>
    @foreach($driverslicense_types as $type)
        <div class="col-md-3">
            {!! Form::checkbox('driverslicense_type'.$type,'1', false, ['id'=>'driverslicense_type'.$type, 'class' => 'checkbox-style']) !!}
            {!! Form::label('driverslicense_type'.$type, $type, ['id'=>'driverslicense_type'.$type, 'class'=>'checkbox-style-3-label']) !!}
        </div>
    @endforeach
</div>

So now i don't know how to save this in my controller, anyone could help?

  • 写回答

1条回答 默认 最新

  • drz5553 2017-01-31 14:41
    关注

    The first thing to remember is that checkboxes that are not checked on the form are NOT posted to the script.

    Second, if you put the required value in the value="" attribute rather than the default of 1 that you are using you will get some useful data back from the form

    Start with your array of types and give each type code the correct value.

    /* ABCD
     *
     * A: 0 No 1 AM 2 A1 3 A2 4 A;
     * B: 0 No 1 B1 2 B 3 BE
     * C: 0 No 1 C1 2 C1E 3 C 4 CE
     * D: 0 No 1 D1 2 D1E 3 D 4 DE
     */
    
    $dl_types = [
        'aaas' => ['AM'=>1, 'A1'=>2, 'A2'=>3, 'A'=>4],
        'bees' => ['B1'=>1, 'B'=>2, 'BE'=>3],
        'cees' => ['C1'=>1, 'C1E'=>2, 'C'=>3, 'CE'=>4],
        'dees' => ['D1'=>1, 'D1E'=>2, 'D'=>,3 'DE'=>4]
    ]
    

    Now use this to load a value into the value="" attribute on the form for each of the checkboxes.

    My Laravel is very rusty, so please check this and dont beat me up if I made a syntax error here

    @foreach($dl_types as $name => $subarray)
        @foreach ($subarray as $type => $value)
            <div class="col-md-3">
            {!! Form::checkbox($name.'[]',$value, false, ['id'=>'driverslicense_type'.$type, 'class' => 'checkbox-style']) !!}
            {!! Form::label('driverslicense_type'.$type, $type, ['id'=>'driverslicense_type'.$type, 'class'=>'checkbox-style-3-label']) !!}
            </div>
        @endforeach
    @endforeach
    

    Now when the form is posted you should get 4 arrays like this in $_POST

    $_POST['aaas'][....]
    $_POST['bees'][....]
    $_POST['cees'][....]
    $_POST['dees'][....]
    

    The sub arrays of each of these will contain the values of the checked checkboxes you placed in the value="" attribute for each one.

    So as you say under another answer, you only need the largest from each category to get the answer you want regardless of how many aaas for example were checked, you can then do this for each category

    $result_string = '';        // init the string
    
    if ( !isset($_POST['aaas'] ) {
        // nothing in the A set was checked
        $result_string .= '0';
    } else {
        $result_string .= $_POST['aaas'][count($_POST['aaas']-1);
    }
    
    if ( !isset($_POST['bees'] ) {
        $result_string .= '0';
    } else {
        $result_string .= $_POST['bees'][count($_POST['bees']-1);
    }
    
    if ( !isset($_POST['cees'] ) {
        $result_string .= '0';
    } else {
        $result_string .= $_POST['cees'][count($_POST['cees']-1);
    }
    
    if ( !isset($_POST['dees'] ) {
        $result_string .= '0';
    } else {
        $result_string .= $_POST['dees'][count($_POST['dees']-1);
    }
    

    You should end up with a result string containing 4 numeric characters that you can store onto your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳