0

I have a table called subject_scores which has the following fields:

candidateNumber, paperCode, paperNumber, question1, question2, ... , question20

The issue is displaying it in the view (CodeIgniter), here's my code:

<table class="table table-striped table-condensed" id="example">
     <thead>
     <tr>
         <th>Candidate Number</th>
         <th>Paper Code</th>
         <th>Paper Number</th>
         <?php for ($i = 1; $i <= 20; $i++) : ?>
         <th><?php echo 'Q'.$i; ?></th>
         <?php endfor; ?>
         <th>Total</th>
     </tr>
     </thead>
     <tbody>
     <?php if (isset($scores)) : foreach ($scores as $row) : ?>
     <tr>
         <td><?php echo $row->candidateNumber; ?></td>
         <td><?php echo $row->paperCode; ?></td>
         <td><?php echo $row->paperNumber; ?></td>
         <td><?php echo $row->status; ?></td>

         <?php for ($i = 1; $i <= 20; $i++) : ?>
         <?php if ($row->question.$i == NULL) : ?>
         <td><span class="red">NA</span></td>
         <?php else : ?>
         <td><?php echo $row->question.$i; ?></td>
         <?php endif; ?>
         <?php endfor; ?>
     </tr>
     <?php endforeach; endif; ?>
     </tbody>
</table>

From the above code you'll notice I've used something like this $row->question.$i to combine the array value with the increment to create fields from question1 to question20.

How do I do this correctly either in PHP or CodeIgniter?

h4kl0rd
  • 625
  • 1
  • 10
  • 23
  • Your if statement will never be null since you're appending a number to it every time before checking it. – xd6_ Aug 27 '14 at 22:38
  • I'm not trying to append a number. If you read at the bottom, I've explained why I'm doing that. I want to retrieve fields from question1 to question20, by not printing them manually. – h4kl0rd Aug 27 '14 at 22:42
  • possible duplicate of [Get PHP class property by string](http://stackoverflow.com/questions/804850/get-php-class-property-by-string) – omma2289 Aug 27 '14 at 23:07
  • 1
    Have you tried $row->{"question{$I}"} ? – xd6_ Aug 28 '14 at 22:09

0 Answers0