Jump to content

How to make progress bar focus on most recent value set?


mal14

Recommended Posts

I'm in the middle of producing a calories tracker. My users can set weight loss goals and then use the exercise calorie calculator to find out how many calories they've lost from particular exercises. I've produced a progress bar to show users their progress, however the progress bar does this for all of the users goals combined, not the most recent goal set. 

 

How could I set my progress bar to focus on just the most recent goal set by the user?

 

This is what I have so far

<?php 
$con=mysqli_connect("localhost","root","password","registration"); 
if (mysqli_connect_errno()) 
{ 
    die("Failed to connect to MySQL: " . mysqli_connect_error()); 
}


$bar_length = 500;
$bar_height = 40;


$sql = "SELECT 
          SUM(calories) as calories
          , CASE g.weightunit
              WHEN 'lbs' THEN 3500
              ELSE 7700
            END * weightlost as target_calories
        FROM goal g 
            INNER JOIN tracklog t USING (userid)
        WHERE userid = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param('i', $_SESSION['userid']);
$stmt->execute();
$stmt->bind_result($calories, $target);
$res = $stmt->fetch();


function progress_bar($width, $height, $calories, $target)
{
    $bar = "<p style='padding-left: 30px'>"."<svg width='$width' height='$height' align='center' view_box='0 0 $width $height'>\n
            <rect x='0' y='0' width='$width' height='$height' align='right' fill='#CCC' />\n";
    // calc width of bar for calories already burned
    if ($target==0) {
            $target = 1;
    }
    if ($calories > 0) {
        $cal_width = $calories * $width / $target;
        $bar .= "<rect x='0' y='0' align='center' width='$cal_width' height='$height' stroke='#ccc' fill='#55d5e7' />\n";
    }
    else {
        $bar .= "<text x='5' y='16' align='center' font-size='9pt'>No data for user</text>\n";
    }
    $bar .= "</svg>\n";
    return $bar;
}
mysqli_close($con);
?>


    <p align="center" class="font-3" style="font-size: 16px"><br><br >Calories burned to date : <?=$calories?><br>
    Target calories : <?=$target?><br>
    <?= progress_bar($bar_length, $bar_height, $calories, $target) ?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.