Reputation: 103
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
Upvotes: 1
Views: 83
Reputation: 1333
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>
Upvotes: 1