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 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?