为什么我的数组洗牌两次而不是一次?



我创建了一个关于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'
],
];
}

然后my formType:

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
]);
}
}

最后是控制器:

<?php
namespace AppController;
use AppFormQuizControllerType;
use AppServiceHttpCodeService;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class QuizController extends AbstractController
{
#[Route('/', name: 'app_quiz')]
public function index(Request $request): Response
{
$session = $request->getSession();
$form = $this->createForm(QuizControllerType::class);
$indiceQuestion = $request->query->get('question', 0);
if (0 === $indiceQuestion) {
$session->clear();
$questionsList = InformationCodeService::list();
dump(1,$questionsList);
shuffle($questionsList);
dump(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')) > $indiceQuestion ? $session->get('questionsList')[$indiceQuestion]['http_message'] : '';
$form->handleRequest($request);
if ('' === $message) {
dump('je passse');
$results = [];
$responses = $session->get('responses');
$questionsList = $session->get('questionsList');
for ($i = 0; $i < count($responseFromUser); $i++) {
if ($responseFromUser[$i] === $responses[$i]) {
$results[$i] = $responseFromUser[$i];
}
}
$score = count($results). ' / '. count($questionsList);
$session->set('score', $score);
return $this->redirectToRoute('app_quiz_finish');
}
dump(3,$session->get('questionsList'));
if ($form->isSubmitted() && $form->isValid()) {
dd($session->get('questionsList'));
$response = $form->getData()['response'];
$responseFromUser[] = $response;
$session->set('responseFromUser', $responseFromUser);
$indiceQuestion++;
dd($indiceQuestion);
return $this->redirectToRoute('app_quiz', ['question' => $indiceQuestion]);
}
return $this->render('quiz/index.html.twig', [
'form' => $form->createView(),
'message' => $message,
'indice_question' => $indiceQuestion,
'total_question' => count($session->get('questionsList'))
]);
}
#[Route('/finish', name: 'app_quiz_finish')]
public function finish(): Response
{
return $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再次打印我的数组shuffle:

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,但这一次与前一页相同的数组,数组不再洗牌。
也就是说我的数组被shuffle了两次

  • 第一个当我到达页面
  • 当我提交第一个表单时

但我只希望数组洗牌一次当我到达localhost页面

我不知道它为什么会这样,如果你知道的话,那将是一种乐趣

当您提交表单时,您不传递question参数。所以代码

$indiceQuestion = $request->query->get('question', 0);

设置默认值$indiceQuestion = 0,再次进入数组洗牌的条件。

您可以将条件转换为

if (0 === $indiceQuestion && !$form->isSubmitted())

或检查其他条件,如$session->get('questionsList')

中的值是否存在。

相关内容

  • 没有找到相关文章

最新更新