内联css中的PHP变量


PHP variable inside the inline css

我需要显示每个用户的电子学习进度。该值是使用PHP从数据库中检索的。我使用的进度条带有bootstrap,它要求我将进度值放在内联css中的width属性中。这可能吗?这是我尝试过的,但没有显示任何内容:

代码

<?php
            include('../../dbconnect.php');
            $id = $_SESSION['ID'];
            $sql = "SELECT * FROM progress WHERE lectureID=1 AND chapterID = 1 AND currentLevel='novice' AND ID = '$id';";
            $query = mysqli_query($con, $sql) or die('Query failed');
            while ($result = mysqli_fetch_array($query)) {
                $value = $result['chapterPerc'];
                ?>
                <div class="progress-bar progress-bar-striped active" role="progressbar"
                     aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:<?php echo $value ?> %;"> <!--tried this-->
                </div>
                <?php
            } //while loop closes here
            ?>
        </div>

任何帮助都将不胜感激,谢谢。

您不需要循环,因为您打印的是单行:

<?php
$sql = "SELECT * FROM progress WHERE lectureID='1'  
        AND currentLevel='novice' AND ID = '$id'";//; removed
$query = mysqli_query($con, $sql) or die('Query failed');  
$result = mysqli_fetch_array($query);
?>
<div class="progress-bar progress-bar-striped active" role="progressbar"
 aria-valuenow="<?php echo $result['chapterPerc'];?>" 
 aria-valuemin="0" aria-valuemax="100" 
 style="width:<?php echo $result['chapterPerc'];?>%;"> 
</div>

希望这能奏效。我已经测试过了,它运行良好。

按以下方式尝试

style="width:<?php echo $value.'%'; ?>;"

试试这个:

<?php
            include('../../dbconnect.php');
            $id = $_SESSION['ID'];
            $sql = "SELECT * FROM progress WHERE lectureID=1 AND chapterID = 1 AND currentLevel='novice' AND ID = '$id';";
            $query = mysqli_query($con, $sql) or die('Query failed');
            while ($result = mysqli_fetch_array($query)) {
                $value = $result['chapterPerc'];
                ?>
                <div class="progress-bar progress-bar-striped active" role="progressbar"
                     aria-valuenow="<?php echo $value; ?>" aria-valuemin="0" aria-valuemax="100" style="width:<?php echo $value; ?>%;">
                </div>
                <?php
            } //while loop closes here
            ?>
        </div>

它应该有效,因为你没有在aria-valuenow属性中指定宽度

此外,试着制作print_r($value);,以确保它是你的例外。

"问题出在查询本身——没有章节ID 1应该写2。感谢您的真知灼见。:)–达米尼2小时前。"

您应该使用or die(mysqli_error($con))而不是or die('Query failed')来获得实际错误。