Php 如何得到两个变量之间百分比不同的结果
我有一个包含不同事件的数据库,其中包括最大参与人数 我有一个页面,向人们展示我们举办的活动,并带有一个到inscripe表单的链接。 所有事件的铭文都放在同一张表中,并有一列记录了它们所记录的事件。ATM显示有多少地方,有多少人已经订阅了。如果铭文数量等于参与者的最大数量,它也会阻止新的铭文。情况如下:Php 如何得到两个变量之间百分比不同的结果,php,percentage,Php,Percentage,我有一个包含不同事件的数据库,其中包括最大参与人数 我有一个页面,向人们展示我们举办的活动,并带有一个到inscripe表单的链接。 所有事件的铭文都放在同一张表中,并有一列记录了它们所记录的事件。ATM显示有多少地方,有多少人已经订阅了。如果铭文数量等于参与者的最大数量,它也会阻止新的铭文。情况如下: <h2 class="title"> <?php //
<h2 class="title">
<?php
//display eventname
$sql = "SELECT eventname FROM events WHERE id=1";
$data = $conn->query($sql);
if ($data->num_rows > 0) {
while($event = $data->fetch_assoc()) {
echo $event["eventname"];
$eventname = $event["eventname"];
}
}
?>
</h2>
<h2 style="color:red;">
<?php
//display registered participants
$sql = ("SELECT COUNT(*) FROM eventregistrations WHERE eventname='$eventname'");
$rs = mysqli_query($conn, $sql);
$result = mysqli_fetch_array($rs);
echo $result[0].'/';
$numberparticipants = $result[0];
// display max participants
$sql = "SELECT maxparticipants FROM events WHERE id=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["maxparticipants"];
$maxparticipants = $row["maxparticipants"];
}
}
?>
</h2>
为什么不像这样
if ($numberparticipants < 80% of $maxparticipants) {
echo 'free places';
} else if ($numberparticipants BETWEEN 80% of $maxparticipants AND $maxparticipants ) {
echo 'almost full';
} else {
echo 'full';
}
$eightyPercent = intval(0.8 * $maxparticipants);
if($eightyPercent < $numberparticipants && $numberparticipants < $maxparticipants){
echo 'almost full';
}
$eightyPercent=intval(0.8*$maxparticipants);
if($eightyPercent<$numberparticipants&$numberparticipants<$maxparticipants){
回声“几乎满了”;
}
$sql=(“从注册中选择计数(*),其中eventname=“$eventname”);
$rs=mysqli_查询($conn,$sql);
$result=mysqli\u fetch\u数组($rs);
$numberparticipants=$result[0];
$sql=“从id=1的事件中选择maxparticipants”;
$result=$conn->query($sql);
如果($result->num_rows>0){while($row=$result->fetch_assoc()){
$maxampartments=$row[“maxampartments”];}
$p80maxparticipants=($maxparticipants*0.8);
if($numberparticipants<$p80maxparticipants){
回音“开放题字”;
}否则如果($numberparticipants>=$p80maxparticipants&$numberparticipants<$maxparticipants){
回声“最饱满”;
}else if($numberparticipants==$maxparticipants){
回声“伏尔泽特”;
}否则{
回声“错误”;
}
到底是什么问题?不知道如何在PHP中正确计算百分比?80%的情况是,在PHP中,在
之间乘以.8
的值不作为比较运算符存在。但是你在这里不需要它,因为这个条件的第一部分已经被你最初的IF覆盖了。如果其中的值小于80%,则ELSE分支中的值不会小于80%。$numberparticipants>=$p80maxparticipants&
部分是多余的。初始的if
检查了$numberparticipants<$p80maxampartipants
是否已经是这种情况,因此在下面的else if
中,除了$numberparticipants>=$p80maxampartipants
之外,什么都可以是这种情况。
$sql = ("SELECT COUNT(*) FROM registrations WHERE eventname='$eventname'");
$rs = mysqli_query($conn, $sql);
$result = mysqli_fetch_array($rs);
$numberparticipants = $result[0];
$sql = "SELECT maxparticipants FROM events WHERE id=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {while($row = $result->fetch_assoc()) {
$maxparticipants = $row["maxparticipants"];}}
$p80maxparticipants = ($maxparticipants *0.8);
if ($numberparticipants < $p80maxparticipants) {
echo 'Open for inscription';
}else if ($numberparticipants >= $p80maxparticipants && $numberparticipants < $maxparticipants) {
echo 'allmost full';
}else if ($numberparticipants == $maxparticipants) {
echo 'Volzet';
}else{
echo 'error';
}