2012-12-26 36 views
-2

我想以專業的方式顯示我的數據,縮短代碼長度,看起來像professional.I不想重複代碼。如何以適當的方式在php中顯示過濾器數據

下面我粘貼我的代碼。如果任何機構有任何最好的主意,請幫助我。

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

什麼專供方式?你能更具體一點嗎? –

+0

我敢打賭,支付的金額看起來不錯,左對齊,根據數據類型可能沒有格式化,不能記住閱讀太多標題爲「amount_paid」的財務文件。但是,如上所述更具體地說明你認爲是錯誤的。 – PeterJ

回答

0

如果你不想在這裏重複自己,這是你的特定情況的某種方法。

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

<?php $items = array(); ?> 

<?php 
    if(!empty($labour)) 
     $items[] = array(
      'columns' => array(
       'name', // column 1 
       'amount_paid', // column 2 
       'date' // column 3 
     ), 
      'data' => $labour['LabourCashcredit'] 
    ); 

    if(!empty($machines)) 
     $items[] = array(
      'columns' => array(
       'first_name', // column 1 
       'amount_paid', // column 2 
       'date' // column 3 
     ), 
      'data' => $machines['MachinePayment'] 
    ); 

    // and so on ... 
?> 

<?php foreach($items as $key => $item_data): ?> 
    <tr> 
     <td><?php echo $counter // think about some counter here ?></td> 
     <?php foreach ($item_data['columns'] as $column): // looping through each column ?> 
      <td><?php echo $item_data['data'][$column] // echoing column value ?></td> 
     <?php endforeach ?> 
    </tr> 
<?php endforeach ?> 

<table> 
相關問題