如何在视图文件中为不同的学生创建单独的表


How to make a separate table for separate student in the view file

我有学生在数据库和他们的收费表。我得到的所有费用的学生在两个日期内提交。目前,我正在显示每一行的结果。有些学生有多个记录,因为他们在这些天之间提交了多个费用。但是我想在单独的表中显示每个学生的数据。

This is Current Design Pic

我想要这些表在设计Pic

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Voucher</th>
      <th>Name</th>
      <th>Amount</th>
      <th>Net Amount</th>
      <th>Month</th>
      <th>Issue Date</th>
      <th>Due Date</th>
      <th>Status</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <?php 
      $paidcount = 0;
      $unpaidcount = 0;
      $unpaidamount = 0;
      $totalsum = 0;
      $payablesum = 0;
      $count = 1; 
      foreach ($this->vouchers as $key => $voucher) {  ?>
        <tr>
          <td><?php echo $count++; ?></td>
          <td><?php echo $voucher['id']; ?></td>
          <td><?php echo ucwords($voucher['name']); ?></td>
          <td>
            <?php
              $totalsum+=$voucher['total'];
              echo $voucher['total'];
            ?>                            
           </td>
           <td>
             <?php
               $payablesum+=$voucher['payable'];
               echo $voucher['payable'];
             ?>            
            </td>
            <td>
              <?php echo date("F", mktime(0, 0, 0, $voucher['id_month'], 10)); ?>
            </td>
            <td><?php echo $voucher['issue_date']; ?></td>
            <td><?php echo $voucher['due_date']; ?></td>
            <td>
              <?php if($voucher['paid']==1) {
                $paidcount+=$voucher['paid'];
                echo "Paid";
                } else {
                  $unpaidamount = $voucher['payable'];
                  $unpaidcount++;
                  echo "Pending";
                } ?>
              </td>
              <td class="text-center">
                <a href="<?php echo SITEURL."vouchersinfo/?action=voucherDetails&id_voucher=".$voucher['id']; ?>" title="View Voucher Details"><span class="glyphicon glyphicon-info-sign"></span></a>
              </td>
            </tr>
             <?php } ?>
            <tr>
              <td colspan="2"><strong>Total Vouchers: </strong><?php echo --$count; ?>
              </td>
              <td colspan="1"><strong>Received: </strong><?php echo $paidcount; ?>
              </td>
              <td colspan="1"><strong>Unpaid: </strong><?php echo $unpaidcount; ?>
              </td>
              <td colspan="2"><strong>Total Amount: </strong><?php echo $totalsum; ?>
              </td>
              <td colspan="2"><strong>Paid Amount: </strong><?php echo $payablesum; ?>
              </td>
              <td colspan="2"><strong>Pending Amount: </strong><?php echo $unpaidamount; ?>
              </td>
            </tr>
          </tbody>
        </table>

尝试一下(我还没有测试它,所以如果有任何典型的错误告诉我编辑代码)

  1. 我们只用一个php块
  2. 不是在单个变量中收集所有状态值,而是在数组的单独索引中收集每个名称的状态
  3. 对于每个凭证,我们有一个表行元素并将其推送到named_rows[$voucher['name']]
  4. 中的数组中。最后,我们有另一个循环来打印我们的表,请阅读代码中的注释

<!-- you shoul use only one php block <?php ?> -->
<?php
    $paidcount = Array(); // use arrays instead to separate each value for only unique name
    $unpaidcount = Array();
    $unpaidamount = Array();
    $totalsum = Array();
    $payablesum = Array();
    $count = 1;
    $name_count = 0;
    // this array holds all of the rows of each table by "name"
    $named_rows = Array();
    // keep an instance of table for future use
    $table_tag_start = '
<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Voucher</th>
      <th>Name</th>
      <th>Amount</th>
      <th>Net Amount</th>
      <th>Month</th>
      <th>Issue Date</th>
      <th>Due Date</th>
      <th>Status</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>';
    $table_tag_end = '
  </tbody>
</table>';
    // now search and separate each name then create new table for each one
    foreach ($this->vouchers as $key => $voucher)
    {
        if (!array_key_exists($voucher['name'], $named_rows)) {
            $named_rows[$voucher['name']] = new Array();
        }
        $totalsum[$voucher['name']] += $voucher['total'];
        $payablesum[$voucher['name']] += $voucher['payable'];
        if($voucher['paid']==1) {
            $paidcount[$voucher['name']] += $voucher['paid'];
        }
        else {
            $unpaidamount[$voucher['name']] = $voucher['payable'];
            $unpaidcount[$voucher['name']]++;
        }
        $row = '<tr>
            <td>' . ($count++) . '</td>
            <td>' . $voucher['id'] . '</td>
            <td>' . ucwords($voucher['name']) . '</td>
            <td>' . $voucher['total'] . '</td>
            <td>' . $voucher['payable'] . '</td>
            <td>' . date("F", mktime(0, 0, 0, $voucher['id_month'], 10)) . '</td>
            <td>' . $voucher['issue_date'] . '</td>
            <td>' . $voucher['due_date'] . '</td>
            <td>' . (($voucher['paid']==1)?'Paid':'Pending') . '</td>
            <td class="text-center"><a href="' . SITEURL . 'vouchersinfo/?action=voucherDetails&id_voucher=' . $voucher['id'] .'" title="View Voucher Details">
                <span class="glyphicon glyphicon-info-sign"></span></a></td>
        </tr>';
        array_push($named_rows[$voucher['name']], $row);
    }
    // for each name, stored in "$named_rows" create a new table and for each row inside "$named_rows[current_name]" create rows
    // when rows are ended, then generate the last row (sum displays) as a new special row, and close the table
    foreach ($named_rows as $key1 => $value1)
    {
        $output = $table_tag_start;
        foreach ($named_rows[$key1] as $value2)
        {
            $output .= $value2;
        }
        $output .= '<tr>
              <td colspan="2"><strong>Total Vouchers: </strong>'.(--$count).'</td>
              <td colspan="1"><strong>Received: </strong>'.$paidcount[$key1].'</td>
              <td colspan="1"><strong>Unpaid: </strong>'.$unpaidcount[$key1].'</td>
              <td colspan="2"><strong>Total Amount: </strong>'.$totalsum[$key1].'</td>
              <td colspan="2"><strong>Paid Amount: </strong>'.$payablesum[$key1].'</td>
              <td colspan="2"><strong>Pending Amount: </strong>'.$unpaidamount[$key1].'</td>
            </tr>';
        $output .= $table_tag_end;
        echo $output;
    }
?>