我有数据库中的学生和他们的费用表。我得到两个日期内提交的学生的所有费用。目前我正在连续显示每个结果。一些学生有多个记录,因为他们在那些日子之间提交了多个费用。但我想在单独的表格中显示每个学生的相同数据。
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)
尝试一下(我还没有测试过,所以如果有任何典型的错误,请告诉我编辑代码)
<!-- 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;
}
?>