T
T
tokyodead2021-06-22 11:09:02
PHP
tokyodead, 2021-06-22 11:09:02

How do I shuffle the items that I take from the database?

There is a testing system, everything works, but I would like to mix the answer options, how can I do this?

<div class="content">
    <?php if( isset($test_data) ): ?>
        <p>Всего вопросов: <?=$count_questions?></p>

        <span class="none" id="test-id"><?=$test_id?></span>
    <div class="test-data">
      <?php foreach($test_data as $id_question => $item): //получаем каждый конкретный вопрос + ответы. ?>
        <div class="question" data-id="<?=$id_question?>" id="question-<?=$id_question?>">
          <?php foreach($item as $id_answer => $answer): //Проходимся по массиву вопрос ответы. ?>
              <?php if( !$id_answer ): //Выводим вопрос ?>
                <p class="q"><?=$answer?></p>
              <?php else: //Выводим варианты ответов ?>
                <p class="a">
                  <input type="radio" id="answer-<?=$id_answer?>" name="question-<?=$id_question?>" value="<?=$id_answer?>">
                  <label for="answer-<?=$id_answer?>"><?=$answer?></label>
                </p>
              <?php endif;  //$id_answer ?>
          <?php endforeach ?>
        </div><!-- .question -->
      <?php endforeach; //test_data ?>
    </div> <!-- .test_data  -->


I looked at what can be done through shuffle, but my $answer is displayed as a string
. I tried explode(", ", $answer) But then only one answer comes out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Page-Audit.ru, 2021-06-22
@PageAuditRU

shuffle( $item ); foreach($item as $id_answer => $answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question