2014-08-28 46 views
0

我已經在PHP中創建了一個對象(我的第一個工作的對象之一)。我需要在頁面上多次返回以下代碼......從php對象編碼的方法返回html和php

<?php while($section = mysql_fetch_array($section3)){ ?> 
      <tr width='150'> 
      <td><?php echo $section['EmProSectName'];?></td> 
      <td><?php 
    echo $sect2->FindStatusRow($_SESSION['cEmployee'], $section['idEmProSect']); 

    ?></td> 
      <td><?php echo $section['EmProSectBTN']; ?></td> 
      </tr> 

      <?php 
    $checkSect = $sect2->checkSubSection($section['idEmProSect']); 
    if($checkSect==1){ 

    $subSect = $sect2->getSubSection($section['idEmProSect']); 
    while($sect = mysql_fetch_array($subSect)){ 
    ?> 
      <tr> 
      <td class="indent30"><?php echo $sect['EmProSectName'];?></td> 
      <td><?php 

      echo $sect2->FindStatusRow($_SESSION['cEmployee'], $sect['idEmProSect']); 
    ?></td> 
      <td><?php echo $sect['EmProSectBTN']; ?></td> 
      </tr> 

      <?php }?> 
      <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      </tr> 
      <?php 
     } 
    } ?> 

我正要寫一個方法來返回上面的語句。東西沿線

public function showEmployProcessresult($sectName){ 
     // here would be the table statement 
    } 

但是我不知道如何編寫我的塊在中間,然後正確地返回它。有人可以告訴我怎麼樣?

+0

爲什麼不創建一個由這些php元素連接的字符串,然後回顯出整個字符串,而不是在html元素中間回顯php。然後在你的'showEmployProcessresult()'函數中,你將生成並返回字符串,然後回顯它! – James 2014-08-28 11:28:33

回答

0

在這裏,我去掉了$ sectName變量,因爲它沒有被使用,並且將空的td結合在一起。

public function showEmployProcessresult(){ 

     $block = ''; 

     while($section = mysql_fetch_array($section3)){ 

      $block .= '<tr width='150'>'; 
      $block .= '<td>'; 
      $block .= $section['EmProSectName'] 
      $block .= '</td>'; 
      $block .= '<td>'; 

      $block .= $sect2->FindStatusRow($_SESSION['cEmployee'], $section['idEmProSect']); 

      $block .= '</td>'; 
      $block .= '<td>'; 
      $block .= $section['EmProSectBTN']; 
      $block .= '</td>'; 
      $block .= '</tr>'; 

      $checkSect = $sect2->checkSubSection($section['idEmProSect']); 

      if($checkSect==1){ 

      $subSect = $sect2->getSubSection($section['idEmProSect']); 

      while($sect = mysql_fetch_array($subSect)){ 

       $block .= '<tr>'; 
       $block .= '<td class="indent30">'.$sect['EmProSectName'].'</td>'; 
       $block .= '<td>'; 

       $block .= sect2->FindStatusRow($_SESSION['cEmployee'], $sect['idEmProSect']); 

       $block .= '</td>'; 
       $block .= '<td>'.$sect['EmProSectBTN'].'</td>'; 
       $block .= '</tr>'; 

      } 

      $block .= '<tr>'; 
      $block .= '<td colspan="3">&nbsp;</td>'; 
      $block .= '</tr>'; 
     } 
    } 

    return $block; 
} 
+0

哇謝謝我沒有意識到這是那麼容易 – 2014-08-28 11:36:54

+0

不客氣!享受你的時間與PHP對象,它可以變得很有趣:) – 2014-08-28 11:37:42