簡體   English   中英

百分比計算,未提供確切值

[英]Percentage calculation, doesn't give the exact value

我有一個由表格和圖像作為表格組成的進度條的問題。

這是表的形成

<td><?php echo $img1; ?></td>
<td><?php echo $img2; ?></td>
<td><?php echo $img3; ?></td>
<td><?php echo $img4; ?></td>
<td><?php echo $img5; ?></td>

這是PHP代碼:

<?php
$value = 10000 //this value is obtained by SQL
$bar1 = 2600 //is the value of the first bar, obtained by SQL
$bar2 = 9950 //is the value of the second bar, obtained by SQL
$bar3 = 13010 //same as before
$bar4 = 17500 //same as before
$bar5 = 19500 //same as before

if($value > 0) && ($value < $bar1) {
$percent = ($value / $bar1) *100;
$bar1 = '<img src="progress.png" width="'.$percent.'" />';
}

if($value >= $bar1) {
$percent = '100';
$bar1 = '<img src="reached.png" width="'.$percent.'" />';
?>

並且對於第一個小節正常,按預期工作,如果該值小於$ bar1量,則將其顯示為進度,如果高於$ bar1量,則顯示為達到。

問題從其他欄開始,以欄3為例:

<?php 
if($value > $bar2) && ($value < bar3) {
$percent = ($value / $bar3) *100;
$bar3 = '<img src="progress.png" width="'.$percent.'" />';
}
>?

顯然,百分比的響應是:76%,幾乎填滿了所有標准。 事實是前一個柱線設置為9950,其值是10000,因此應在$ bar3(即13010)上顯示類似(在30%之間)而不是76%。

目的很簡單:

當$ value填滿$ bar1時,它繼續前進並開始填充$ bar2,如果也填滿了它,則移至$ bar3 ..直到$ value不足以填滿或到達最后的$ bar5。

但是,這是“錯誤的”情況。

我在這方面弄錯了什么?

我通過更改操作解決了該問題:

$bar3_reduced = $bar3 - $bar2;
$new = $value - $bar2; //detract from the value the previous bar amount
$percent = ($new / $bar3_reduced)*100;

因此,現在只比較“新”欄中的進度,而不是全部統計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM