2016-11-03 38 views
0

我有學生在數據庫及其費用表中。我收到兩個日期內提交的學生的所有費用。目前我正在連續顯示每個結果。一些學生有多個記錄,因爲他們在那幾天之間提交了多個費用。但是我想在同一個表中顯示每個同一個學生的數據。如何在查看文件中爲獨立學生製作獨立表

This is Current Design Pic

I want these tables in design 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> 
+0

請把更多的完整代碼的註釋中,從** **

開始標籤和它的結束 –

回答

0

試試吧(我沒有測試它,所以告訴我編輯的代碼,如果有任何典型的錯誤)

  1. 我們只使用一個php塊
  2. 代替收集單個變量中的所有狀態值,我們有我們的數組收集e他們的陣列
  3. 每個憑單的單獨的索引內ACH名,我們有一個錶行元素,並將其在named_rows推到一個數組[$憑單[「名稱」]]
  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; 
    } 
?>