samedi 11 février 2023

laravel compare from array in controller with if lease condition

I'm trying to compare array getting from two different inputs and compare it in controller if both value same then pass TRUE if Not pass FALSE.

I tried below method but I was getting error and error pointing to below code

 if ($request->ANSWER[$i] === $request->OPTION[$i]) 
          
                {
                 $data->ANSWER_STATUS = "1"; 
                 } else {
                 $data->ANSWER_STATUS = "0"; 
                 } 

Error Code

Trying to access array offset on value of type null

my array pater looks

 "Question" => array:2 [▼
   0 => "html full form"
   1 => "water formula in science"
  ]
  "ANSWER" => array:2 [▼  //COMPARE 
    0 => "Hypertext Markup Language"
    1 => "h2O"
  ]
  "OPTION" => array:2 [▼  //COMPARE 
    0 => "Markup Language"
    1 => "h2O"
  ]

CONTROLLER

    public function Store_Answer(Request $request)
   {
        $Questioncount= $request->Question;
         
         if ($Questioncount) {
         
         for ($i=0; $i <count($request->Question); $i++) {
         $data = New OnlineExminAnswer();
 
             if ($request->ANSWER[$i] === $request->OPTION[$i]) 
              
                    {
                     $data->ANSWER_STATUS = "1"; 
                     } else {
                     $data->ANSWER_STATUS = "0"; 
                     } 
                     
               $data->question = $request->Question [$i];           
        $data->save();
         } 
         } 
 
        return back()->with('success',' Successfully .');
 
   }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire