我想以专业的方式显示我的数据,以缩短代码长度并看起来像专业。我不想重复代码。
下面我粘贴了我的代码。如果有人有任何最好的主意,请帮助我。
<table>
<?php if(!empty($labour) || !empty($machines) || !empty($materials) || !empty($contractors)){?>
<tr>
<th>Id</th>
<th>Name</th>
<th>amount_paid</th>
<th>date</th>
</tr>
<?php }?>
<?php if(!empty($labour)){
$i=1; foreach($labour as $res):?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $res['LabourCashcredit']['name'];?></td>
<td><?php echo $res['LabourCashcredit']['amount_paid'];?></td>
<td><?php echo $res['LabourCashcredit']['date'];?></td>
</tr>
<?php $i++; endforeach;
}?>
<?php if(!empty($machines)){
$i=1; foreach($machines as $res):?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $res['MachinePayment']['first_name'].' '. $res['MachinePayment']['last_name'];?></td>
<td><?php echo $res['MachinePayment']['amount_paid'];?></td>
<td><?php echo $res['MachinePayment']['date'];?></td>
</tr>
<?php $i++; endforeach;
} ?>
<?php if(!empty($materials)){
$i=1; foreach($materials as $res):?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $res['MaterialPayment']['first_name'].' '. $res['MaterialPayment']['last_name'];?></td>
<td><?php echo $res['MaterialPayment']['paid_amount'];?></td>
<td><?php echo $res['MaterialPayment']['date'];?></td>
</tr>
<?php $i++; endforeach;
} ?>