我創建了一份有關 http 程式碼的問卷,這是 100 程式碼的範例:
public static function list(): array { return [ [ 'http_message' => 'Continue', 'http_code' => '100' ], [ 'http_message' => 'Switching Protocols', 'http_code' => '101' ], [ 'http_message' => 'Processing', 'http_code' => '102' ], [ 'http_message' => 'Early Hints', 'http_code' => '103' ], ]; }
然後是我的表單類型:
class QuizControllerType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('response', TextType::class, [ 'attr' => ['autofocus' => true] ]) ->add('submit', SubmitType::class, array( 'label' => 'Suivant' )) ; } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ // Configure your form options here ]); } }
最後是我的控制器:
getSession(); $form = $this->createForm(QuizControllerType::class); $indiceQuestion = $request->query->get('question', 0); if (0 === $indiceQuestion) { $會話->clear(); $questionsList = InformationCodeService::list(); 轉儲(1,$questionsList); 隨機播放($questionsList); 轉儲(2,$questionsList); $responses = array_column($questionsList, 'http_code'); $session->set('questionsList', $questionsList); $session->set('responses', $responses); $session->set('responseFromUser', []); } $responseFromUser = $session->get('responseFromUser'); $message = \count($session->get('questionsList')) >; $index問題 ? $session->get('questionsList')[$indiceQuestion]['http_message'] : ''; $form->handleRequest($request); if ('' === $訊息) { 轉儲('je passse'); $結果=[]; $responses = $session->get('responses'); $questionsList = $session->get('questionsList'); for ($i = 0; $i < count($responseFromUser); $i ) { if ($responseFromUser[$i] === $responses[$i]) { $結果[$i] = $responseFromUser[$i]; } } $分數 = \count($結果)。 '/'。 \count($questionsList); $session->set('分數', $score); 返回 $this->redirectToRoute('app_quiz_finish'); } dump(3,$session->get('questionsList')); if ($form->isSubscribed() && $form->isValid()) { dd($session->get('questionsList')); $response = $form->getData()['response']; $responseFromUser[] = $response; $session->set('responseFromUser', $responseFromUser); $index問題; dd($indiceQuestion); return $this->redirectToRoute('app_quiz', ['question' => $indiceQuestion]); } 返回 $this->render('quiz/index.html.twig', [ '形式' => $form->createView(), '消息' => $訊息, 'indice_question'=>; $index問題, '總問題' => \count($session->get('questionsList')) ]); } #[路線('/完成',名稱:'app_quiz_finish')] 公共函數 finish(): 回應 { 返回 $this->render('quiz/finish.html.twig'); } }
在 url https://127.0.0.1:8001/
的索引頁上,我的第一個轉儲按順序列印倉庫,轉儲 2 和 3 打亂順序列印:
array:4 [▼ 0 => array:2 [▼ "http_message" => "Early Hints" "http_code" => "103" ] 1 => array:2 [▼ "http_message" => "Processing" "http_code" => "102" ] 2 => array:2 [▼ "http_message" => "Continue" "http_code" => "100" ] 3 => array:2 [▼ "http_message" => "Switching Protocols" "http_code" => "101" ] ]
但是當我提交表單時,dd 再次列印我的陣列隨機播放:
array:4 [▼ 0 => array:2 [▼ "http_message" => "Processing" "http_code" => "102" ] 1 => array:2 [▼ "http_message" => "Switching Protocols" "http_code" => "101" ] 2 => array:2 [▼ "http_message" => "Continue" "http_code" => "100" ] 3 => array:2 [▼ "http_message" => "Early Hints" "http_code" => "103" ] ]
我不明白為什麼要這樣做。如果我刪除dd
,提交表單後,我將使用不同的陣列重定向到https://127.0.0.1:8001/?question=1
,如果我再次提交,我將重定向到https://127.0.0.1 :8001/?question=2
但這次使用與上一頁相同的數組,並且數組不再隨機。
所以這意味著我的陣列被洗牌兩次:
但我只想在到達本地主機頁面時將數組隨機播放一次
我不知道為什麼它會這樣,如果你有任何想法,那將是一種樂趣
當您提交表單時,您沒有傳遞
question
參數。所以程式碼設定預設值
$indiceQuestion = 0
,再次陷入陣列打亂的情況。您可以像這樣轉換條件
或檢查其他條件,例如
$session->get('questionsList')
中是否存在值