簡體   English   中英

如何在視圖文件中為單獨的學生制作單獨的表格

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

我的數據庫中有學生,還有他們的費用表。 我要在兩個日期之內提交學生的所有費用。 目前,我正在連續顯示每個結果。 一些學生有多個記錄,因為他們在那幾天之間提交了多個費用。 但我想在單獨的表中顯示每個相同學生的數據。

這是當前的設計圖片

我想要這些表在設計圖片

<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;
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM