1

I need to display the elearning progress of each user. This value is retrieved from the database using PHP. The progress bar i'm using is with bootstrap and it requires that I put the progress value inside the width property found in the inline css. Is this possible? This is what I've tried but it's not displaying anything:

CODES

<?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>

Any help will be appreciated thanks.

6
  • 1
    Yes. Should work fine. Verified that $value is what you expect? What does the source output look like? Might just be the space before the %? Commented Apr 1, 2016 at 19:16
  • 1
    Try removing the space between <?php echo $value ?> and % in the inline CSS. Commented Apr 1, 2016 at 19:17
  • 1
    Good opportunity to consider starting to use <?= $value ?>... Keeps things neater I find. Just an observation. Commented Apr 1, 2016 at 19:19
  • 1
    Besides comments/answer. Make sure the session was started. Commented Apr 1, 2016 at 19:19
  • 1
    Does it actually return a value? just echo out first the value before putting it in the progress bar. Just to make sure. There may be a return, but if the field you specified is wrong when you assign it to the value variable, (mistakes are easily made) it may come up empty Commented Apr 1, 2016 at 19:35

4 Answers 4

2

You don't need to loop since you are printing a single row:

<?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>

Hope this will work. I have tested and it is working fine.

Sign up to request clarification or add additional context in comments.

2 Comments

It was a simple mistake with the query there's no chapterID 1 in the table. All fixed now. Amended my code to yours. Thanks.
@Damini, please click the tick icon above to accept if it helps.
2

Try as follows

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

5 Comments

Please check it now.
@D4V1D ; isn't needed before ?>.
@Barmar: How come? Every PHP statements must finish with a ; right?
It's a special case, since ?> ends PHP statement parsing, so it also ends the statement.
Fair enough, didn't know that. I still think, even though not needed, ; should be there just to be consistant.
1

Try this:

<?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>

It shoud works, beacause you didn't specified the width in the aria-valuenow property

Also, try about making print_r($value); to be sure it's what you are excepting for.

1 Comment

The aria-valuenow is not mandatory to see the progress bar actually.
1

"The problem was with the query itself there's no chapterID 1 should have written 2. Thanks for the insight. :) – Damini 2 hours ago."

Rather than or die('Query failed') what you should have used was or die(mysqli_error($con)) in order to get the real error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.