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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决