douxing2156 2016-05-12 03:37
浏览 39
已采纳

检索数组名称

I have used array name in form. this routes to the controller function shown below.

  <form method="POST" action="{{url('quiz/check')}}"
{!! csrf_field() !!} 

@foreach ($quiz as $q)

@if($q->level=='1')
{{ $q->qid }}.  
{{ $q->question }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='1'>     
{{ $q->opt1 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='2'>     
{{ $q->opt2 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='3'>    
{{ $q->opt3 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='4'>   
{{ $q->opt4 }}<br><br>
@endif
@endforeach        
    <button type="submit" class="btn btn-default">Get result</button>
                            </form>

which i am trying to retrieve in controller as:

public function check(Request $request)
  {
    $count=0; 
    $input=$request->all();
    $mycheck=$input['mycheck'];
    $ch = DB::select('select * from quiz where category="gk" ');
    return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count]);     
 }

view file check.blade.php

Results:
@foreach ($quiz as $q)

   @if(array_key_exists($q->qid, $mycheck) && $mycheck[$q->qid]==$q->answer)
      {{$count=$count+1}}

@endif

@endforeach        

You scored {{$count}}.

now, i am getting this error:

undefined variable: mycheck in view file

  • 写回答

1条回答 默认 最新

  • duanliaogui4133 2016-05-12 05:40
    关注

    You are not passing $mycheck variable to view.

    Replace this line:

    return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count]);     
    

    With this:

    return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count,'mycheck'=>$mycheck]);   
    

    Also, looks like You trying to access variable $q which is actually variable $quiz according to Your data passed to view.

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失