2014-03-14 40 views
0

我正在嘗試製作一個CSS文件,它基於登錄的用戶改變顏色,但是我似乎無法從CSS文檔內部運行SQL查詢。這樣做否定的CSS ...動態CSS文檔中的MySQL查詢

在主頁的頂部,我有:

<link rel="stylesheet" href="css/dynamictag.php" media="screen"> 

,並在文件看起來像下面的CSS工作:

<?php 
    header("Content-type: text/css; charset: UTF-8"); 
?> 
#colortag { 
    width: 30px; 
    height: 50px; 
    float: left; 
    border-bottom-left-radius: 10px; 
    border-bottom-right-radius: 10px; 
    -webkit-box-shadow: 1px 0px 5px; 
    box-shadow: 1px 0px 5px; 
    margin-left: 5px; 
    margin-top: -5px; 
    background-color: #FFFFFF; 
    margin-right: 5px; 
} 
#theuser { 
    float: left; 
    font-size: small; 
} 

但當我有下列情況時不適用:

<?php 
    header("Content-type: text/css; charset: UTF-8"); 
?> 
<?php 
$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';" 
     $result=mysql_query($query); 
     $row=mysql_fetch_array($result); 
     $color='#'.$row['Color']; 
?> 
#colortag { 
    width: 30px; 
    height: 50px; 
    float: left; 
    border-bottom-left-radius: 10px; 
    border-bottom-right-radius: 10px; 
    -webkit-box-shadow: 1px 0px 5px; 
    box-shadow: 1px 0px 5px; 
    margin-left: 5px; 
    margin-top: -5px; 
    background-color: <?php echo $color;?>; 
    margin-right: 5px; 
} 
#theuser { 
    float: left; 
    font-size: small; 
} 

是否有另一種方法來做到這一點?難道我做錯了什麼?

感謝

+0

也許是緩存問題。嘗試在鏈接標記中使用href =「css/dynamictag.php?<?php rand();?>」。如果沒有,那麼檢查或發佈CSS輸出。 – Ivarpoiss

+0

不幸的是沒有運氣。 – loopback128

+0

然後檢查生成的CSS輸出,看看是否應該如此。如果是,那麼看看「background-color:<?php echo $ color;?>!important;」確實。 – Ivarpoiss

回答

1

下面的行末丟失分號(而是有它的SQL語句中),所以PHP沒有正確執行。

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';" 

它應該是:

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session'"; 

此外,你應該使用庫MySQLi或PDO。在PHP中使用mysql_query已棄用,如mysql_query PHP page所示。

+0

嘆氣,謝謝。我錯過了那個錯覺。 – loopback128

+0

沒問題,發生在我們最好的:) – zongweil