laravel控制器中,如果数组值相同,则返回1,否则返回0。

huangapple go评论57阅读模式
英文:

laravel controller form array value compere if same pass 1 not pass 0

问题

我尝试从控制器中的数组值中获取值并比较所有"ANSWER"和"OPTION"数组值。

示例

如果
ANSWER 0=>A 等于 OPTION=>A 通过 1
ANSWER 0=>B 等于 OPTION=>A 通过 0

这个值存储在ANSWER_STATUS列中。

控制器

public function Store_Answer(Request $request)
{
    $count= $request->Question;
    if ($count) {
        for ($i=0; $i < count($request->Question); $i++) {
            $data = new OnlineExminAnswer();
            $data->ANSWER_STATUS= $request->ANSWER_STATUS; // 在这里我想要获取比较的值1或0
            $data->question = $request->Question[$i];
            $data->answer = $request->OPTION[$i];
            $data->save();
        } 
    }
}

我的表单数组

"Question" => array:2 [
    0 => "YOUR NAME"
    1 => "water formula in science"
]
"ANSWER" => array:2 [  // 这个数组包含所有正确的答案
    0 => "A"
    1 => "h2O"
]
"OPTION" => array:2 [  // 这个数组包含学生的答案
    0 => "A"
    1 => "CO2"
]
英文:

I'm new to laravel I was trying to get value IN controller form array value compere all "ANSWER" AND "OPTION" array value.

EXAMPLE

ANSWER AND OPTION if
ANSWER 0=>A EQUELS OPTION=>A pass 1
ANSWER 0=>B EQUELS OPTION=>A pass 0

THIS VALUE STORE IN ANSWER_STATUS COLUMANE

CONTROLLER

    public function Store_Answer(Request $request)
   {
     $count= $request-&gt;Question;
     if ($count) {
     for ($i=0; $i &lt;count($request-&gt;Question); $i++) {
     $data = New OnlineExminAnswer();
     $data-&gt;ANSWER_STATUS= $request-&gt;ANSWER_STATUS; // HERE I WANT TO GET VALUE OF COMPARED 1 OR 0
     $data-&gt;question = $request-&gt;Question [$i];
     $data-&gt;answer = $request-&gt;OPTION[$i];
     $data-&gt;save();
  } 
     }

my form array

  &quot;Question&quot; =&gt; array:2 [▼
    0 =&gt; &quot;YOUR NAME&quot;
    1 =&gt; &quot;water formula in science&quot;
  ]
  &quot;ANSWER&quot; =&gt; array:2 [▼  //THIS ARRAY CONTAINING ALL RIGHT ANSWER
    0 =&gt; &quot;A&quot;
    1 =&gt; &quot;h2O&quot;
  ]
  &quot;OPTION&quot; =&gt; array:2 [▼  //THIS ARRAY STUDENTS ANSWER 
    0 =&gt; &quot;A&quot;
    1 =&gt; &quot;CO2&quot;
  ]
]

答案1

得分: 1

public function Store_Answer(Request $request)
{
    $count = $request->Question;
    if ($count) {
        for ($i = 0; $i < count($request->Question); $i++) {
            if(isset($request->ANSWER[$i]) && isset($request->OPTION[$i])) {
                $data = new OnlineExminAnswer();
                $data->ANSWER_STATUS = $request->ANSWER[$i] == $request->OPTION[$i] ? 1 : 0;
                $data->question = $request->Question[$i];
                $data->answer = $request->OPTION[$i];
                $data->save();
            }
        }
    }
}
英文:
public function Store_Answer(Request $request)
{
    $count = $request-&gt;Question;
    if ($count) {
        for ($i = 0; $i &lt; count($request-&gt;Question); $i++) {
            if(isset($request-&gt;ANSWER[$i]) &amp;&amp; isset($request-&gt;OPTION[$i])) {
                $data = new OnlineExminAnswer();
                $data-&gt;ANSWER_STATUS = $request-&gt;ANSWER[$i] == $request-&gt;OPTION[$i] ? 1 : 0;
                $data-&gt;question = $request-&gt;Question[$i];
                $data-&gt;answer = $request-&gt;OPTION[$i];
                $data-&gt;save();
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月10日 12:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定