2016-04-22 44 views
0

我有一個php變量,其中包含一個文件夾中可用的磁盤空間值。如何在css中使用php變量

$UsedSpace = $total/20000000*100; 

現在我想用$ UsedSpace的價值狀態欄是這樣的:http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_progressbar_2

所以寬度:1%;在這個例子中現在應該包含變量的值。 我怎樣才能做到這一點?

+8

的可能重複評論[添加一個php變量到一個css語句!](http:// stackoverf low.com/questions/6800263/add-a-php-variable-to-a-css-statement) –

回答

1

審查代碼波紋管,這將幫助,請閱讀HTML部分

$(document).ready(function(){ 
 
\t \t $width=$("#myBar").data('width'); 
 
\t \t $("#myBar").css("width",$width+"%"); 
 
\t });
#myProgress { 
 
    position: relative; 
 
    width: 100%; 
 
    height: 30px; 
 
    background-color: #ddd; 
 
} 
 

 
#myBar { 
 
    position: absolute; 
 
    height: 100%; 
 
    background-color: #4CAF50; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 

 
<h1>My First JavaScript Progress Bar</h1> 
 

 
<div id="myProgress"> 
 
    <div id="myBar" data-width="11"></div> 
 
    <!-- 
 
    for php: set data-width like bellow 
 
    <div id="myBar" data-width="<?php echo '11'; ?>"></div> 
 
    --> 
 
</div> 
 

 
</body>

+0

這個很好用!謝謝你的解決方案 –

+0

歡迎@JackMaessen –

0

把你的變量中HTML/CSS這樣的:<?php echo $variable; ?>

+0

爲什麼選擇投票?這個答案有效。 –

1

您也可以做到這一點使用JQuery。

<script> 
    var UsedSpace = <?php echo $UsedSpace; ?>; 
    $('#myBar').css('width', UsedSpace); 
</script> 
0

計算$UsedSpace和使用它像這樣:

<div id="myProgress"> 
    <div id="myBar"></div> 
</div> 

<?php $UsedSpace = 500; ?> 

<style> 
#myProgress { 
    position: relative; 
    width: 100%; /*total memory size width */ 
    height: 30px; 
    background-color: #ddd; 
} 

#myBar { 
    position: absolute; 
    width: <?php echo $UsedSpace ; ?>; /*UsedSpace width that is highlighted*/ 
    height: 100%; 
    background-color: #4CAF50; 
} 
</style>