在创建页面的某个部分之前激活某些PHP代码

时间:2014-02-20 19:17:54

标签: php html

我正在使用连接到数据库并使用PHP的HTML网页。我在顶部的div中有一个变量,直到文档后面的某些PHP才能正确设置。有没有办法在后面的PHP之后进行div构建,但仍然应该在页面顶部的位置?

这是使用变量的代码

<div id="HeaderRight">         
        <div id="HeaderRight">        
        <?php        
        echo '<a href="../Cart/index.php" class="header">
        <img src="../Images/shoppingcart.gif" height="18" width="127"     border="0" align="ABSMIDDLE" /> Contains '.$_SESSION["num_items"].' Items</a>'
        ?>
     </div>

这是设置变量的代码

<?php

echo '<TABLE WIDTH="100%" BGCOLOR="#FFFFFF" ALIGN = "RIGHT" BORDER = "1">';
echo '<TR BGCOLOR = "818996" HEIGHT = "25"><TD>Product</TD><TD>Availability</TD>               <TD>Qty</TD><TD>Price</TD><TD>Total</TD></TR>';

$strTotalPrice = 0;

for ($i=0; $i<$_SESSION['num_products']; $i++) {

$strSQL = "SELECT * FROM products WHERE ProductCode = '".$_SESSION['cart'][$i]['name']."'";

$rsProd = mysql_query($strSQL)
    or die($db_name . " : " . $strSQL . " : " . mysql_error());

$rowProd = mysql_fetch_array($rsProd);


$strSQLDetails = "SELECT * FROM pagedetails WHERE Category = '".$rowProd["Category"]."'";
$rsDetails = mysql_query($strSQLDetails)
    or die($db_name . " : " . $strSQLDetails . " : " . mysql_error());

$rowDetails = mysql_fetch_array($rsDetails);
// End Of "Retrieve Desired Record Set"

echo ' <TR HEIGHT = "' .$rowProd["ThumbnailHeight"]. '" ><TD style="vertical-align:middle"><img src="../'.$rowProd["Department"].'/Images/'.$rowProd["ProductCode"].'_sm.jpg" align = "left" border="0" height = "'.$rowProd["ThumbnailHeight"].'" width="100" hspace="25" />
      <br><a href = "../Products/Products.php?PageID='.$rowDetails["PageID"].'" >'.$rowProd["ProductName"].'</a>
       </TD>';

$strAvailability = "";
$strQuantity = 0;
if($rowProd["Stock"] < $_SESSION["cart"][$i]["qty"]){
    $strAvailability = "Only " .$rowProd["Stock"]. " in Stock, Quantity Changed";
    $strQuantity = $rowProd["Stock"];
    $_SESSION["cart"][$i]["qty"] = $rowProd["Stock"];
}
else{
    $strAvailability = "In Stock";
    $strQuantity = $_SESSION["cart"][$i]["qty"];
}


echo '<TD ALIGN = "center">'.$strAvailability.'</TD>';
echo '<TD ALIGN = "center">
<input type="text" name="txtQuantity" size="2" maxlength="2" value ='.$strQuantity.'>';
echo '<form action="index.php?=?product='.$rowProd["ProductCode"].'&quantity=1" method="post"> 
<input type="submit" name="submit" value="Update"> 
</form> 
</TD>';

$strPrice = $rowProd["RegPrice"] * $strQuantity;
$strTotalPrice += $strPrice;

echo '<TD ALIGN = "center">$'.$rowProd["RegPrice"].'.00</TD>';
echo '<TD ALIGN = "center">$'.$strPrice.'.00</TD></TR>';
//echo '( <a href=index.php?product=' . $_SESSION['cart'][$i]['name'] . '&quantity=1>+</a> )';  
}

1 个答案:

答案 0 :(得分:0)

正如Rocket Hazmat所指出的,在渲染HTML之前运行所有PHP代码是个好主意。如果不重写当前代码,可以使用output buffering

来实现
<?
// add this block
function firstDiv() {
  ob_start();
?>

<div id="HeaderRight">         
  ... <? echo $_SESSION["num_items"]; ?> ...
</div>

<?
 // ... and this block
   $_SESSION["firstDiv"] = ob_get_contents();
   ob_end_clean();
} // end of firstDiv
?>

与页面的其他部分相同

<?
function restOfYourPage() {
  ob_start();
?>

<TABLE> ... <? $_SESSION["num_items"] = 666; ?> ... </TABLE>

<?
  $_SESSION["restOfYourPage"] = ob_get_contents();
  ob_end_clean();
} // end of restOfYourPage
?>

然后你可以按相反的顺序运行php代码,但按正常顺序显示:

<?
 restOfYourPage();
 firstDiv();
 echo $_SESSION["firstDiv"];
 echo $_SESSION["restOfYourPage"];
?>