【发布时间】:2014-08-12 14:36:28
【问题描述】:
我第一次尝试使用 .php 文件而不是 .css 来设置我的代码样式,但无法让一个变量起作用。
我正在使用 $scale 变量来确定评分等级有多大,我得到了很好的答案(用 echo $scale 测试它;)但是由于某种原因,我正在制作的 div 的高度$scale 没有改变。这就是我得到 $scale 变量的方式:
$sql = "SELECT votes_number, votes_sum FROM lessons WHERE id='$lesson_id'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$total_votes = $row["votes_number"];
$total_upvotes = $row["votes_sum"];
$scale = ceil(100-(($total_upvotes / $total_votes) * 100));
echo $scale;
这是文件的html:
<div id='rating_meter'><div><div></div></div></div>
CSS(不工作)部分:
#rating_meter{
background-color:#D3D3D3;
width:5px;
height:200px;
display:inline-block;
float:left;
border-bottom:1px solid #7E7E7E;
}
#rating_meter > div{
background-color:green;
width:5px;
height:180px;
display:inline-block;
}
#rating_meter > div > div{
background-color:red;
width:5px;
display:inline-block;
height:<?php echo $scale;?>%;
}
当我使用 echo $scale;要确定高度,它总是将高度设置为 100%,但它会将我 81 回显到屏幕顶部,所以它应该是 81%...当我使用数字而不是回显 $scale 时,例如
height:73%;
或者在第一个 php 文件中设置 $scale 如下:
$scale = "25";
一切正常... 我怎样才能让我的 $scale 作为声明 div 高度的变量工作? 这个问题真的花了我很多时间来解决......
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.