PHP extract()在以后的代码需要结果之前没有完成

时间:2013-10-20 22:10:04

标签: php

我的页面顶部有一些PHP,它从数据库中读取默认的css值:

$query = "
              SELECT 
                width,
                height,
                fontFamily,
                fontSize,
                color,
                backgroundColor,
                lineHeight,
                marginTop,
                marginLeft,
                textAlign,
                borderColor,
                borderWidth,
                boxShadow,
                borderRadius,
                last_url_published,
                color1,
                color2,
                color3,
                color4,
                color5,
                color6
              FROM 
                users
              WHERE 
                id = :id
              ";  
    $query_params = array (
                           ':id' => $_SESSION['user']['id']
    );

//  echo("<br />index.php: query for defaults is: $query");
    try
    {
            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);
    }
    catch(PDOException $ex)
    {
        die("index.php line " . __LINE__ . ". Failed to read defaults from users table: "  . $ex->getMessage());
    }

    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $row = $stmt->fetch();


    extract($row);      // produces $width, etc. 

//  echo("<br />killing time here");

在HTML中我有

<body>

.cell_db {
    width: <?=$width?>;
    height: <?=$height?>;
    background-color: <?=$backgroundColor?>;
    border-color: <?=$borderColor?>;
    border-width: <?=$borderWidth?>;
    border-radius:<?=$borderRadius?>;
    box-shadow:<?=$boxShadow?>
}

我所看到的是,如果我不放

         echo("<br />killing time here");

在提取($ row)语句后输入,css值 - 如宽度:不被赋予。例如,摘录中的$ width值($ row)显然没有准备好css行由PHP评估。但是从echo语句中插入延迟可以提供所需的时间。有谁知道这里发生了什么以及如何使这项工作成功?

由于

Addendem:

下面的人要求查看从PHP到HTML的所有代码,所以这里是:

<?php 
//phpinfo();
ini_set('display_errors', 1);
error_reporting(E_ALL);
include ('functions.php');  // so we can call our showarray()


    // First we execute our common code to connect to the database and start the session

    require("common.php");

//  showarray($_SESSION, "index.php, line " . __LINE__ . " SESSION Data:");


    $_SESSION['landingPage'] = $_SERVER['PHP_SELF'];  // login will redirect to here, either index.php or indexX.php, after password matches

    // At the top of the page we check to see whether the user is logged in or not.

    if(empty($_SESSION['user']))
    {
        // If they are not, we redirect them to the login page.

        header("Location: login.php");

        // Remember that this die statement is absolutely critical.  Without it,
        // people can view your members-only content without logging in.

        die("Redirecting to login.php");
    }





// get box default style for this user from the database
// update default styles
    $query = "
              SELECT 
                width,
                height,
                fontFamily,
                fontSize,
                color,
                backgroundColor,
                lineHeight,
                marginTop,
                marginLeft,
                textAlign,
                borderColor,
                borderWidth,
                boxShadow,
                borderRadius,
                last_url_published,
                color1,
                color2,
                color3,
                color4,
                color5,
                color6
              FROM 
                users
              WHERE 
                id = :id
              ";  
    $query_params = array (
                           ':id' => $_SESSION['user']['id']
    );

//  echo("<br />index.php: query for defaults is: $query");
    try
    {
            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);
    }
    catch(PDOException $ex)
    {
        die("index.php line " . __LINE__ . ". Failed to read defaults from users table: "  . $ex->getMessage());
    }

    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $row = $stmt->fetch();

    extract($row);      // produces $width, etc. 

    sleep(1);

//  echo("<br />killing time here");


// open_color picker  - in color.js
// open color panel  - in color.js


?>
<!doctype html> 
<!-- HTML5  -->
<html>
<head>
<meta charset="utf-8">
<title>MyWebsheet</title>



<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script src="js/GLOBALS.js"></script>  <!-- Load these before editbox.js and color.js so they can see them when compiled  -->
<script src="js/weebly_editbox.js"></script>
<script src="js/color.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/jquery.Jcrop.js"></script>
<script src="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?key= Fmjtd%7Cluub2h0z21%2C7a%3Do5-9utaqr"></script>

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
<!-- <link rel="stylesheet" type="text/css" href="CSS/reset.css" /> -->
<link rel="stylesheet" type="text/css" href="CSS/jquery.Jcrop.css" />



<!-- ***************************************************************************************************** -->
<!--                                                STYLES                                                 -->
<!-- ***************************************************************************************************** -->

<style type="text/css">  
/* style top */

body {      /* style wings  wings style  body color*/



    height:4000px;

    background-color:<?php echo ($_SERVER['PHP_SELF'] == '/indexX.php'? 'white' : '#165E3D')?>;  
    background-color:#fff;
    background-color:#d44;  /* change to make sure a new index.php is being uploaded  */
    margin:0;
    padding:0px;  /* keeps everything 10px away from walls  */

}
/*
a {
    cursor : pointer;
}  */

p {
    /*  margin-top: 10px;  */
}


#wrapper {
    width:1024px;
    height:800px;
    position: absolute;
    left:50%;                   /* puts left edge of wrapper at mid-point of viewport   */
    margin-left: -512px;        /* shifts wrapper left half of its width  */
    background-color:transparent;
}
.cell_base {
    position:absolute;
    z-index:3;
    border-style: solid;
    display:none;
}

.cell_db {
    width: <?=$row['width']?>;
    height: <?=$row['height']?>;
    background-color: <?=$backgroundColor?>;
    border-color: <?=$borderColor?>;
    border-width: <?=$borderWidth?>;
    border-radius:<?=$borderRadius?>;
    box-shadow:<?=$boxShadow?>
}
.cellContent_db {
    font-family: <?=$fontFamily?> ;
    margin-top: <?=$marginTop?>;
    margin-left: <?=$marginLeft?>;
    font-size: <?=$fontSize?>;
    color: <?=$color?>; 
    line-height: <?=$lineHeight?>;
    text-align: <?=$textAlign?>;
}

0 个答案:

没有答案