【问题标题】:I need this variable to take to view for comparing我需要这个变量来查看比较
【发布时间】:2018-01-10 17:56:57
【问题描述】:

我有这个代码

foreach ($test_results as $question_result) {
    if($question_result == 1){
        $correct_question =1;
    }
    else
    {
        $correct_question =0;
    }
}

到目前为止,当我尝试使用 dd($correct_question); 时这很好,它会告诉我它是否正确,但我希望它显示在我有这个视图的视图中

@foreach($test_questions as $question)
    @foreach ($test_results as $question_result)
        @if($correct_question)
            <i id="checki" class="fa fa-check"></i> 
        @else
            <i id="remove" class="fa fa-remove"></i>
        @endif
    @endforeach
@endforeach

我不知道如何用这样的东西发送到视图,或者我不知道任何人都可以帮助我..?谢谢。

return view('website.tests.take.take-test-results')->with([
    'results'          => $test_results,
    'test_questions'   => $test_questions,
    'correct_question' => $correct_question,
]);

【问题讨论】:

  • 退后一步,检查您的代码逻辑。这没有道理。试着一步一步地向我们解释。这通常可以帮助您弄清楚自己在哪些方面做得更好。

标签: php laravel view foreach blade


【解决方案1】:

您可以简单地按照以下方式进行:

 @foreach ($test_results as $question_result)
    @if($question_result == 1)
        <i id="checki" class="fa fa-check"></i> 
    @else
        <i id="remove" class="fa fa-remove"></i>
    @endif
@endforeach

【讨论】:

    【解决方案2】:

    试试下面的代码:

    @foreach($test_questions as $question)
        @if(in_array($question, $correct_question))
            <i id="checki" class="fa fa-check"></i> 
        @else
            <i id="remove" class="fa fa-remove"></i>
        @endif
    @endforeach
    

    注意:如果您的 $question 有 id 并且您的 $correct_question 有正确的问题/答案数组,您可以尝试 @if(in_array($question['id'], $correct_question))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多