我正在制作卡路里追踪器。我的用户可以设定减肥目标,然后使用运动卡路里计算器找出他们从特定运动中丢失了多少卡路里。我已经制作了一个进度条来向用户显示他们的进度,但是进度条为所有用户目标组合而不是最近的目标集。
如何设置进度条以专注于用户设置的最新目标?
这是我到目前为止所拥有的
<?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) ?>