0

I am creating an exam report in php using codeigniter and postgresql which displays the questions and the number of students who correctly answered the questions and the number of students who answered the questions incorrectly. When I tried displaying the data, it only prints one row when in fact I have 3 rows. I used print_r() to know what are the elements of my array and it looks like this: RESULT: print_r()

But then when I tried displaying it in views, it turns out like this: Result of displaying in View

I have these in my controller:

public function loadViewExamResult(){
    $this->load->model('exam_model');
    $exam_no= $this->uri->segment(3);
    $data['answers'] = $this->exam_model->correctWrong($exam_no);
    print_r($data['answers']) ;

    $this->load->view('exam_report_chart',$data);

}

Model:

 <?php 
    foreach ($answers as $row) {
      $question = $row->question;
      $correct = $row->correct;
      $wrong = $row->wrong;
    }
 ?>


 <div class="row">
   <div class="col-lg-12">
    <section class="panel">
     <header class="panel-heading">
         EXAM STATISTICS
     </header>
     <div class="panel-body text-center">
      <table class="table table-striped table-advance table-hover">
        <tbody>
         <tr>
          <th>QUESTION</th>
          <th>No. of Correct</th>
          <th>No. of Wrong</th>
         </tr>

         <tr>
          <td><?php echo $question;?></td>      
          <td><?php echo $correct;?></td>
          td><?php echo $wrong;?></td>
         </tr>

       </tbody>
      </table>
     </div>
    </section>
   </div>
  </div>

How do I solve this?

1 Answer 1

2

The issue is here:

foreach ($answers as $row) {
      $question = $row->question;
      $correct = $row->correct;
      $wrong = $row->wrong;
}

on each iteration the last value in $question, $correct, $wrong is over written. Make them array so that they can hold multiple value like:

foreach ($answers as $row) {
      $question[] = $row->question;
      $correct[] = $row->correct;
      $wrong[] = $row->wrong;
}

or put the table tr inside the loop like:

foreach ($answers as $row) {
?>
<tr>
  <td><?php echo $row->$question;?></td>      
  <td><?php echo $row->$correct;?></td>
  td><?php echo $row->$wrong;?></td>
</tr>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Sounds great !!

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.