2012-11-19 73 views
0

我對「高級PHP」很新穎,並且遇到PHP頭文件錯誤。已經發送了PHP頭文件

<?php 
     $getTrans = dbquery("SELECT * FROM cms_transactions WHERE userid = '".USER_ID."' AND pin = '0' ORDER BY date DESC LIMIT 50"); 
     $oe = 1; 
     while($transaction = mysql_fetch_assoc($getTrans)){ 
      if ($oe == 2) 
      { 
       $oe = 1; 
      } 
      else 
      { 
       $oe = 2; 
      } 
      $oDate = strtotime($transaction['date']); 
      $sDate = date("<b>l, j - F, o</b> g:ha",$oDate); 
      echo' 
       <tr style=" background-color: ' . (($oe == 2) ? '#e2e2e2' : '#fff') . ';"> 
       <td>'.$transaction['id'].'</td> 
       <td>'.$transaction['descr'].'</td> 
       <td>'.$transaction['credits'].' deducted</td> 
       <td>'.$transaction['amount'].' deducted</td> 
       <td>'.$transaction['vip_points'].' deducted</td> 
       <td>'.$sDate.'</td> 
       <td><input type="submit" value="+ Pin" name="'.$transaction['id'].'" /></td> 
       </tr> 
      '; 
    $trans = $transaction['id']; 
    if(isset($_POST["$trans"])){ 
     dbquery("UPDATE cms_transactions SET pin = '1' WHERE id = '".$trans."' AND userid = '".USER_ID."' LIMIT 1"); 
     header("Location: ".WWW."/identity/transactions"); 
    } 
     } 
     if(mysql_num_rows($getTrans) == 0){ 
     echo '<div class="error" id="black">You have no recent Transactions</div>'; 
     } 

    ?> 

我在/身份證/交易的底部不斷收到PHP頭錯誤。

我意識到我得到的問題是因爲「頁面已經加載內容」,並且必須在頁面的開頭使用PHP頁眉。 我發佈的以下片段是包含的文件;在主文件中。

主要問題是,我正在尋找一種方法將內容從「後端」php工作中分離出來。

因此,例如:

主要PHP文件具有的全部代碼,和包括();文件具有「內容」方面。

這有可能嗎?如果你能舉一個例子,那會很棒!

請讓我知道,如果有什麼不清楚給你。

編輯;這是整個文件:

<table class="information-table" style="margin-top:-5px;"> 
<tr> 

<td style="text-shadow:none;"> 

<a class="new-button" href="<?php echo WWW ; ?>/account/logout"><b>Log out</b><i></i></a> 

     <div id="back-link" style="color:#fff;"> 

     <a href="<?php echo WWW ; ?>/identity/avatars" style="color:#fff;">My Avatars</a> &raquo; <a href="<?php echo WWW ; ?>/identity/settings" style="color:#fff;">Account Settings</a> &raquo; Transactions 

     </div> 


</td></tr> 
</table> 


<div id="column1" class="column" style="width:960px;margin-right:10px;"> 

     <h1>Account Transactions:</h1> 
     <h3>Pinned Transactions:</h3> 
     <i>Pinning a transaction is useful if you have a transaction that is important; that you might need in the future!</i> 
     <p> 
     <table> 
     <thead> 
     <tr><td>ID</td><td>Description</td><td>Credits</td><td>Pixels</td><td>Stars</td><td>Purchase Date</td><td></td></tr> 
     </thead> 
     <tbody> 
     <form method="post"> 
     <?php 
      $getTrans = dbquery("SELECT * FROM cms_transactions WHERE userid = '".USER_ID."' AND pin = '1' ORDER BY date DESC LIMIT 100"); 
      $oe = 1; 
      while($transaction = mysql_fetch_assoc($getTrans)){ 
       if ($oe == 2) 
       { 
        $oe = 1; 
       } 
       else 
       { 
        $oe = 2; 
       } 
       $oDate = strtotime($transaction['date']); 
       $sDate = date("<b>l, j - F, o</b> g:ha",$oDate); 
     $transs = $transaction['id']; 
     if(isset($_POST["un$transs"])){ 
      dbquery("UPDATE cms_transactions SET pin = '0' WHERE id = '".$transs."' AND userid = '".USER_ID."' LIMIT 1"); 
      header("Location: ".WWW."/identity/transactions"); 
     } 
       echo' 
        <tr style=" background-color: ' . (($oe == 2) ? '#e2e2e2' : '#fff') . ';"> 
        <td>'.$transaction['id'].'</td> 
        <td>'.$transaction['descr'].'</td> 
        <td>'.$transaction['credits'].' deducted</td> 
        <td>'.$transaction['amount'].' deducted</td> 
        <td>'.$transaction['vip_points'].' deducted</td> 
        <td>'.$sDate.'</td> 
        <td><input type="submit" value="- Pin" name="un'.$transaction['id'].'" /></td> 
        </tr> 
       '; 
      } 
      if(mysql_num_rows($getTrans) == 0){ 
      echo '<div class="error" id="black">You have no Pinned Transactions</div>'; 
      } 

     ?> 
     </form> 
     </tbody> 
     </table> 
     </p> 

     <h3>Recent Transactions</h3> 
     <p> 
     <table> 
     <thead> 
     <tr><td>ID</td><td>Description</td><td>Credits</td><td>Pixels</td><td>Stars</td><td>Purchase Date</td><td></td></tr> 
     </thead> 
     <tbody> 
     <form method="post"> 
     <?php 
      $getTrans = dbquery("SELECT * FROM cms_transactions WHERE userid = '".USER_ID."' AND pin = '0' ORDER BY date DESC LIMIT 50"); 
      $oe = 1; 
      while($transaction = mysql_fetch_assoc($getTrans)){ 
       if ($oe == 2) 
       { 
        $oe = 1; 
       } 
       else 
       { 
        $oe = 2; 
       } 
       $oDate = strtotime($transaction['date']); 
       $sDate = date("<b>l, j - F, o</b> g:ha",$oDate); 
     $trans = $transaction['id']; 
     if(isset($_POST["$trans"])){ 
      dbquery("UPDATE cms_transactions SET pin = '1' WHERE id = '".$trans."' AND userid = '".USER_ID."' LIMIT 1"); 
      header("Location: ".WWW."/identity/transactions"); 
     } 
       echo' 
        <tr style=" background-color: ' . (($oe == 2) ? '#e2e2e2' : '#fff') . ';"> 
        <td>'.$transaction['id'].'</td> 
        <td>'.$transaction['descr'].'</td> 
        <td>'.$transaction['credits'].' deducted</td> 
        <td>'.$transaction['amount'].' deducted</td> 
        <td>'.$transaction['vip_points'].' deducted</td> 
        <td>'.$sDate.'</td> 
        <td><input type="submit" value="+ Pin" name="'.$transaction['id'].'" /></td> 
        </tr> 
       '; 
      } 
      if(mysql_num_rows($getTrans) == 0){ 
      echo '<div class="error" id="black">You have no recent Transactions</div>'; 
      } 

     ?> 
     </form> 
     </tbody> 
     </table> 
     </p> 

</div> 
<style> 
td { 
padding:10px; 
} 

tbody td { 
text-shadow:0 1px 0 #fff; 
} 

thead td { 
font-weight:bold; 
text-align:center; 
} 
</style> 
+0

有一堆關於這個問題已經:[這裏](http://stackoverflow.com/a/8028987/184124)即將與解決方案 – HorusKol

+0

通常使用的方法問題的一個很好的,詳細的解答首先是做頁面處理;在完成之後,包含**內容文件**,並簡單地在內容中使用處理結果。 –

回答

0

您可以使用Output buffering,在這種情況下,防止已經發出標題:

ob_start(); 
$getTrans = dbquery("SELECT * FROM cms_transactions WHERE userid = '".USER_ID."' AND pin = '0' ORDER BY date DESC LIMIT 50"); 
     $oe = 1; 
     while($transaction = mysql_fetch_assoc($getTrans)){ 
      if ($oe == 2) 
      { 
       $oe = 1; 
      } 
      else 
      { 
       $oe = 2; 
      } 
      $oDate = strtotime($transaction['date']); 
      $sDate = date("<b>l, j - F, o</b> g:ha",$oDate); 
      echo' 
       <tr style=" background-color: ' . (($oe == 2) ? '#e2e2e2' : '#fff') . ';"> 
       <td>'.$transaction['id'].'</td> 
       <td>'.$transaction['descr'].'</td> 
       <td>'.$transaction['credits'].' deducted</td> 
       <td>'.$transaction['amount'].' deducted</td> 
       <td>'.$transaction['vip_points'].' deducted</td> 
       <td>'.$sDate.'</td> 
       <td><input type="submit" value="+ Pin" name="'.$transaction['id'].'" /></td> 
       </tr> 
      '; 
    $trans = $transaction['id']; 
    if(isset($_POST["$trans"])){ 
     dbquery("UPDATE cms_transactions SET pin = '1' WHERE id = '".$trans."' AND userid = '".USER_ID."' LIMIT 1"); 
     header("Location: ".WWW."/identity/transactions"); 
    } 
     } 
     if(mysql_num_rows($getTrans) == 0){ 
     echo '<div class="error" id="black">You have no recent Transactions</div>'; 
     }  

ob_end_clean(); 
0

我在重構你的代碼有一條縫,把所有的處理時頁面的頂部,以及底部的顯示(使您遇到的錯誤發生的可能性小得多)。

<?php 

// I always do most of my PHP processing before any presentation stuff 
// Makes for more manageble code 

// Init the Variables 
// Transaction Arrays 
$pinned_transactions = array(); 
$unpinned_transactions = array(); 
// Interaction Variables 
$ids = array(); 
$action = false; 

// Check for Unpin/Pin Request 
if(isset($_POST['unpin']) && count($_POST['unpin'])){ 

    // There is a request to Unpin a Transaction 
    $ids = array_keys($_POST['unpin']); 
    $action = 0; 

}elseif(isset($_POST['pin']) && count($_POST['pin'])){ 
    // There is a request to Pin a Transaction 
    $ids = array_keys($_POST['pin']); 
    $action = 1; 
} 

// There was some kind of Pin/Unpin Request 
if(count($ids)){ 

    // Process the Change 
    dbquery('UPDATE cms_transactions SET pin = '.(int) $action.' WHERE id IN('.implode(',' , $ids).') AND userid = '.(int) USER_ID); 

    // Set the Redirect 
    $redirect_to = WWW.'/identity/transactions'; 

    // Use Headers, if we can 
    if(!headers_sent()) 
    header('Location: '.$redirect_to); 
    // Outputting a javascript Redirect (as a fallback) 
    // along with a link (if javascript is off). 
    echo '<script type="text/javascript">document.location.href = "'.$redirect_ro.'";</script>'; 
    echo '<a href="'.$redirect_to.'">Click here to continue</a>'; 
    die(); 

} 

// Perform a Query for Pinned Transactions 
$getTrans = dbquery('SELECT * FROM cms_transactions WHERE userid = '.(int) USER_ID.' AND pin = 1 ORDER BY date DESC LIMIT 100'); 
while($t = mysql_fetch_assoc($getTrans)){ 

    $oDate = strtotime($t['date']); 
    $sDate = date("<b>l, j - F, o</b> g:ha" , $oDate); 
    $t['date'] = $sDate; 

    $pinned_transactions[] = $t; 
} 

// Perform a Query for Unpinned Transactions 
$getTrans = dbquery("SELECT * FROM cms_transactions WHERE userid = '".USER_ID."' AND pin = '0' ORDER BY date DESC LIMIT 50"); 
while($t = mysql_fetch_assoc($getTrans)){ 

    $oDate = strtotime($t['date']); 
    $sDate = date("<b>l, j - F, o</b> g:ha" , $oDate); 
    $t['date'] = $sDate; 

    $unpinned_transactions[] = $t; 
} 


?><table class="information-table" style="margin-top:-5px;"> 
    <tr> 
    <td style="text-shadow:none;"> 
     <a class="new-button" href="<?php echo WWW; ?>/account/logout"><b>Log out</b><i></i></a> 
     <div id="back-link" style="color:#fff;"> 
     <a href="<?php echo WWW; ?>/identity/avatars" style="color:#fff;">My Avatars</a> &raquo; <a href="<?php echo WWW; ?>/identity/settings" style="color:#fff;">Account Settings</a> &raquo; Transactions 
     </div> 
    </td> 
    </tr> 
</table> 

<div id="column1" class="column" style="width:960px;margin-right:10px;"> 

    <h1>Account Transactions:</h1> 

    <h3>Pinned Transactions:</h3> 
    <i>Pinning a transaction is useful if you have a transaction that is important; that you might need in the future!</i> 
    <p> 
<?php if(count($pinned_transactions)){ ?> 
    <form method="post"> 
     <table> 
     <thead> 
      <tr><td>ID</td><td>Description</td><td>Credits</td><td>Pixels</td><td>Stars</td><td>Purchase Date</td><td></td></tr> 
     </thead> 
     <tbody> 
<?php foreach($pinned_transactions as $k => $r){ ?> 
      <tr class="<?php echo ($k%2 ? 'odd' : 'even'); ?>"> 
      <td><?php echo $transaction['id']; ?></td> 
      <td><?php echo $transaction['descr']; ?></td> 
      <td><?php echo $transaction['credits']; ?> deducted</td> 
      <td><?php echo $transaction['amount']; ?> deducted</td> 
      <td><?php echo $transaction['vip_points']; ?> deducted</td> 
      <td><?php echo $transaction['date']; ?></td> 
      <td><input type="submit" value="- Pin" name="unpin[<?php echo $transaction['id']; ?>]" /></td> 
      </tr> 
<?php } ?> 
     </tbody> 
     </table> 
    </form> 
<?php }else{ ?> 
    <div class="error" id="black">You have no Pinned Transactions</div> 
<?php } ?> 
    </p> 

    <h3>Recent Transactions</h3> 
    <p> 
<?php if(count($unpinned_transactions)){ ?> 
    <form method="post"> 
     <table> 
     <thead> 
      <tr><td>ID</td><td>Description</td><td>Credits</td><td>Pixels</td><td>Stars</td><td>Purchase Date</td><td></td></tr> 
     </thead> 
     <tbody> 
<?php foreach($unpinned_transactions as $k => $r){ ?> 
      <tr class="<?php echo ($k%2 ? 'odd' : 'even'); ?>"> 
      <td><?php echo $transaction['id']; ?></td> 
      <td><?php echo $transaction['descr']; ?></td> 
      <td><?php echo $transaction['credits']; ?> deducted</td> 
      <td><?php echo $transaction['amount']; ?> deducted</td> 
      <td><?php echo $transaction['vip_points']; ?> deducted</td> 
      <td><?php echo $transaction['date']; ?></td> 
      <td><input type="submit" value="- Pin" name="pin[<?php echo $transaction['id']; ?>]" /></td> 
      </tr> 
<?php } ?> 
     </tbody> 
     </table> 
    </form> 
<?php }else{ ?> 
    <div class="error" id="black">You have no recent Transactions</div> 
<?php } ?> 
    </p> 

</div> 
<style> 

td { 
    padding:10px; 
} 

// Odd and Even Rows 
tr.odd { 
    background-color:#FFFFFF; 
} 
tr.even { 
    background-color:#E2E2E2; 
} 

tbody td { 
    text-shadow:0 1px 0 #fff; 
} 

thead td { 
    font-weight:bold; 
    text-align:center; 
} 

</style>