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

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

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列中。

控制器

  1. public function Store_Answer(Request $request)
  2. {
  3. $count= $request->Question;
  4. if ($count) {
  5. for ($i=0; $i < count($request->Question); $i++) {
  6. $data = new OnlineExminAnswer();
  7. $data->ANSWER_STATUS= $request->ANSWER_STATUS; // 在这里我想要获取比较的值1或0
  8. $data->question = $request->Question[$i];
  9. $data->answer = $request->OPTION[$i];
  10. $data->save();
  11. }
  12. }
  13. }

我的表单数组

  1. "Question" => array:2 [
  2. 0 => "YOUR NAME"
  3. 1 => "water formula in science"
  4. ]
  5. "ANSWER" => array:2 [ // 这个数组包含所有正确的答案
  6. 0 => "A"
  7. 1 => "h2O"
  8. ]
  9. "OPTION" => array:2 [ // 这个数组包含学生的答案
  10. 0 => "A"
  11. 1 => "CO2"
  12. ]
英文:

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

  1. public function Store_Answer(Request $request)
  2. {
  3. $count= $request-&gt;Question;
  4. if ($count) {
  5. for ($i=0; $i &lt;count($request-&gt;Question); $i++) {
  6. $data = New OnlineExminAnswer();
  7. $data-&gt;ANSWER_STATUS= $request-&gt;ANSWER_STATUS; // HERE I WANT TO GET VALUE OF COMPARED 1 OR 0
  8. $data-&gt;question = $request-&gt;Question [$i];
  9. $data-&gt;answer = $request-&gt;OPTION[$i];
  10. $data-&gt;save();
  11. }
  12. }

my form array

  1. &quot;Question&quot; =&gt; array:2 [▼
  2. 0 =&gt; &quot;YOUR NAME&quot;
  3. 1 =&gt; &quot;water formula in science&quot;
  4. ]
  5. &quot;ANSWER&quot; =&gt; array:2 [▼ //THIS ARRAY CONTAINING ALL RIGHT ANSWER
  6. 0 =&gt; &quot;A&quot;
  7. 1 =&gt; &quot;h2O&quot;
  8. ]
  9. &quot;OPTION&quot; =&gt; array:2 [▼ //THIS ARRAY STUDENTS ANSWER
  10. 0 =&gt; &quot;A&quot;
  11. 1 =&gt; &quot;CO2&quot;
  12. ]
  13. ]

答案1

得分: 1

  1. public function Store_Answer(Request $request)
  2. {
  3. $count = $request->Question;
  4. if ($count) {
  5. for ($i = 0; $i < count($request->Question); $i++) {
  6. if(isset($request->ANSWER[$i]) && isset($request->OPTION[$i])) {
  7. $data = new OnlineExminAnswer();
  8. $data->ANSWER_STATUS = $request->ANSWER[$i] == $request->OPTION[$i] ? 1 : 0;
  9. $data->question = $request->Question[$i];
  10. $data->answer = $request->OPTION[$i];
  11. $data->save();
  12. }
  13. }
  14. }
  15. }
英文:
  1. public function Store_Answer(Request $request)
  2. {
  3. $count = $request-&gt;Question;
  4. if ($count) {
  5. for ($i = 0; $i &lt; count($request-&gt;Question); $i++) {
  6. if(isset($request-&gt;ANSWER[$i]) &amp;&amp; isset($request-&gt;OPTION[$i])) {
  7. $data = new OnlineExminAnswer();
  8. $data-&gt;ANSWER_STATUS = $request-&gt;ANSWER[$i] == $request-&gt;OPTION[$i] ? 1 : 0;
  9. $data-&gt;question = $request-&gt;Question[$i];
  10. $data-&gt;answer = $request-&gt;OPTION[$i];
  11. $data-&gt;save();
  12. }
  13. }
  14. }
  15. }

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:

确定