在PHP中为每个循环附加变量

时间:2019-01-02 13:05:10

标签: php

我有以下代码,该代码显示了表格中的刻度和x(第4个td)。我想计算滴答声和返回的x的数量,以便可以将它们用于图表。我根本没有在php方面的经验,因此将不胜感激。我当时想在foreach语句外声明一个变量,然后每次都附加它,但是tick / x的代码让我失望了!

谢谢

<table class="table table-hover table-striped" >
                                    <tr>
                                        <th>Student Name</th>
                                        <th>Date Assigned</th>
                                        <th>Deadline</th>
                                        <th>Status</th>
                                    </tr>
                                    <?php $ncmpt=0; $cmpt=0;
                                    if (isset($homework_assignment_track) && count($homework_assignment_track) > 0) {
                                        foreach ($homework_assignment_track as $key => $row) {
                                            ?>
                                            <tr>
                                                <td><?= $row->student->name; ?></a></td>
                                                <td><?= date_format(new DateTime($row->assignment->created_at), 'd-M-Y'); ?></td>
                                                <td><?= $row->assignment->valid_date == '1' ? 'Open' : date_format(new DateTime($row->assignment->deadline), 'd-M-Y'); ?></td>
                                                <td><?= (($row->assignment->valid_date == '1') ? '<i class="fa fa-check fa-fw"></i>' : ((isset($row->homeworkCompleteNotification->created_at) && $row->homeworkCompleteNotification->created_at < $row->assignment->deadline) ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times fa-fw"></i>')); ?></td>
                                            </tr>
                                            <?php
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td colspan="4" style="text-align:center;">No Record Found!</td>
                                        </tr>
                                        <?php
                                    } 
                                    ?>
                                </table>



enter code here

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助。

if (isset($homework_assignment_track) && count($homework_assignment_track) > 0) {
  $x=0;
  foreach ($homework_assignment_track as $key => $row) {
  ?>
  <tr>
      <td><?= $row->student->name; ?></a></td>
      <td><?= date_format(new DateTime($row->assignment->created_at), 'd-M-Y'); ?></td>
      <td><?= $row->assignment->valid_date == '1' ? 'Open' : date_format(new DateTime($row->assignment->deadline), 'd-M-Y'); ?></td>
      <td><?= (($row->assignment->valid_date == '1') ? '<i class="fa fa-check fa-fw"></i>' : ((isset($row->homeworkCompleteNotification->created_at) && $row->homeworkCompleteNotification->created_at < $row->assignment->deadline) ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times fa-fw"></i>')); ?></td>
  </tr>
<?php
}
foreach ($homework_assignment_track as $key => $row) {
if($row->assignment->valid_date == '1') x++;
}

您可以根据需要使用x的值