gpt4 book ai didi

javascript - 百分比计算不适用于我的 php

转载 作者:行者123 更新时间:2023-11-29 18:13:47 28 4
gpt4 key购买 nike

首先,我知道百分比计算是这个论坛中经常问到的问题,我已经阅读了其中的大部分内容来解决我的百分比计算问题,但看起来效果不太好。我一直在提到这个

表单代码:

<center><form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php } ?>


<p><strong>Tarikh: *</strong>
<input type="text" style="text-transform:uppercase" name="date" value=" <?php echo $date; ?>"/><br/></p>
<p><strong>Di bawah 60 Minit&nbsp;: *</strong> <input type="text" name="casesolved_u"
value="<?php echo $casesolved_u; ?>"/><br/></p>
<p><strong>Jumlah kes : *</strong> <input type="text" name="casesolved_a"
value="<?php echo $casesolved_a; ?>"/></p>
<p><strong> Peratusan : </strong> <input type="text" name="percentage"
value="<?php echo ($percentage, 2); ?>"/></p>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form></center>

这是我插入新数据的代码

if (isset($_POST['submit']))
{
// get the form data
$date = strtoupper($_POST['date']);
$casesolved_u = htmlentities($_POST['casesolved_u'], ENT_QUOTES);
$casesolved_a = htmlentities($_POST['casesolved_a'], ENT_QUOTES);
$percentage = htmlentities($_POST['percentage'], ENT_QUOTES);

// check all fields are filled
if ($date == '' || $casesolved_u == '' ||$casesolved_a == '' || $percentage == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($date, $casesolved_u, $casesolved_a, $percentage, $error);
}
else
{
// insert the new record into the database
if ($stmt= $mysqli->prepare ("INSERT emergency (date, casesolved_u, casesolved_a, percentage) VALUES (?,?,?,?)"))

{
// calculate percentage
$casesolved_u = $row ['casesolved_u'];
$casesolved_a = $row ['casesolved_a'];
$percentage = ($casesolved_u/$casesolved_a)* 100;
echo ($percentage,2);

$stmt->bind_param("ssss",$date, $casesolved_u, $casesolved_a, $percentage);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}

// redirec the user
header("Location: view.php");
}

在此之前,我手动计算百分比,以便将信息存储在数据库中,但现在,我想让系统尝试自己计算它,但我失败了。希望有人可以帮助我,我再次很抱歉提出重复的问题,因为我真的找不到适合我的解决方案。谢谢您,再次抱歉。

编辑:这是我现在数据库中的内容,因此对于 12 月,我希望自动进行计算,而不再需要手动计算。 enter image description here

最佳答案

项目总数为100%。

如果项目总数为 57,则为 100%。所以每个单位(每1个案例)可以表示为100%/57 = 1,754%

从这里您可以计算 0 到 57 之间任意数量单位的百分比。

例如,11 个案例将为 11 * 1,754% = 19,29%

同样的道理,57个案例是57 * 1,754% = 99,98% (100%)

关于代码:

首先从 $_POST 读取的这两个变量被未在任何地方声明的 $row 覆盖。然后用它们来计算百分比。

$casesolved_u = $row ['casesolved_u'];
$casesolved_a = $row ['casesolved_a'];

如果需要使用 $_POST 的输入来完成计算,请删除这两行,将这些字符串变量转换为数字,然后进行计算。

关于javascript - 百分比计算不适用于我的 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193840/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com