【问题标题】:How to get different outcomes of two variables being different percentage to eachother如何获得两个变量彼此不同百分比的不同结果
【发布时间】:2021-06-07 04:31:52
【问题描述】:

我有一个包含不同事件的数据库,其中包括最大参与者数 我有一个页面向人们展示我们有什么活动,并带有一个到铭文表格的链接。 所有事件的铭文都放在同一张表中,并带有一列它们铭刻的事件。 ATM 它显示有多少地方以及已经订阅了多少人。如果铭文的数量等于最大参与者数量,它也会阻止新的铭文。如下所示:

           <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>

$numberparticipants 和 $maxparticipants 稍后用于更改到 inscripeform 的链接

我想要实现的是页面不显示确切的数字,而只是显示事件有多满的想法。类似的东西

      if ($numberparticipants < 80% of $maxparticipants) {
      echo 'free places';
      } else if ($numberparticipants BETWEEN 80% of $maxparticipants AND $maxparticipants ) {
      echo 'almost full';
      } else {
      echo 'full';
      }

谢谢 博杰

【问题讨论】:

  • 究竟是什么问题?不知道如何在 PHP 中正确计算百分比?嗯 80% 的东西,就是那个东西乘以 .8
  • BETWEEN 在 PHP 中不作为比较运算符存在。但是无论如何您在这里都不需要它,因为您的初始 IF 已经涵盖了该条件的第一部分。如果那里的值

标签: php percentage


【解决方案1】:

为什么不喜欢

$eightyPercent = intval(0.8 * $maxparticipants);
if($eightyPercent < $numberparticipants && $numberparticipants < $maxparticipants){
      echo 'almost full';
}

【讨论】:

    【解决方案2】:
                $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';
                }
    

    【讨论】:

    • $numberparticipants &gt;= $p80maxparticipants &amp;&amp; 部分是多余的。最初的if 检查$numberparticipants &lt; $p80maxparticipants 是否已经是这种情况,所以在下面的else if 只有$numberparticipants &gt;= $p80maxparticipants 可以 是这种情况。
    猜你喜欢
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2016-08-29
    • 2021-10-31
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多