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.

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

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常