1

Basically what I have are two JSON files. One JSON file contains a set of questions and the second JSON file contains the user answers. What I want to do is echo the questions from the one json file and echo out the answers from the other inside a div.

I tried putting a foreach within a foreach. This does work to a certain extent but it echo's the questions multiple times for each answer.

<?php
$questions = "../induction.json";
$contents = file_get_contents($questions,0,null,null);
$qArray = json_decode($contents, JSON_PRETTY_PRINT);

$answers = $user."_induction.json";
$contentsA = file_get_contents($answers,0,null,null);
$qArrayA = json_decode($contentsA, JSON_PRETTY_PRINT);

?>

<div role="tabpanel" class="tab-pane fade" id="messages_with_icon_title">
<b>Quiz</b>
 <?php
    $num = 0;
    foreach($qArray as $key => $value){
       foreach($qArrayA as $key2 => $value2){
 ?>
 <form method="POST">
 <div class="row clearfix">
   <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
     <div class="card">
        <div class="header">
            <h2><?php echo $value['question']; ?></h2>
         </div>
         <div class="body">
         <?php
             echo $value2;
         ?>
      </div>
   </div>
 </div>
</div> 
<?php   } 
} ?>
<input type="submit" class="btn btn-success waves-effect" name="submit" value="Submit" />
</form>
</div>

This is the result: https://i.sstatic.net/PX0V7.jpg

5
  • I guess there is a typo in $value['question'];, it should be $value2['question']; because you are able to echo body content using $value2. Commented Aug 13, 2019 at 16:12
  • the nested foreach seems wrong .. foreach($qArrayA as $key2 => $ Commented Aug 13, 2019 at 16:15
  • Please post correct code. Commented Aug 13, 2019 at 16:15
  • @scaisEdge Fixed that, don't know why it cut that off. Commented Aug 13, 2019 at 16:17
  • 1
    You don't need nested loops. What that does is it'll print out the first questions and then loop through all of the answers. Then the next question is displayed with all of the answers and so on. You need just one loop with a counter. echo $question[$i] ... echo $answer[$i] Commented Aug 13, 2019 at 16:29

1 Answer 1

1

This is what the loop should look like. Note, I pulled the <form> tag out of the loop as it shouldn't have been inside.

<form method="POST">
<?php
for ($i = 0; $i < count($qArray); $i++) 
{
?>
    <div class="row clearfix">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
            <div class="card">
                <div class="header">
                    <h2><?php echo $qArray[$i]; ?></h2>
                </div> <!-- .header -->
                <div class="body">
                    <?php echo $qArrayA[$i]; ?>
                </div> <!-- .body -->
            </div> <!-- .card -->
        </div> <!-- .col-lg-4 -->
    </div> <!-- .row -->
<?php } ?>
<input type="submit" class="btn btn-success waves-effect" name="submit" value="Submit" />
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.