【问题标题】:PHP if not greater than 1, print textPHP如果不大于1,打印文本
【发布时间】:2013-03-25 02:04:45
【问题描述】:

在下面的代码中,我让服务器检查此人是否在团队中。如果是,它会显示一些东西,如果不是,它会显示一些不同的东西。如果这个人在一个团队中,它就可以工作(它打印正确的东西),但如果这个人不在一个团队中,它就不会显示任何东西。有什么帮助吗?

            if($_SESSION["username"]){
                $sql2 = mysql_query("SELECT * FROM teams WHERE game='bl2' AND players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
                $num1 = 0;
                while($row = mysql_fetch_assoc($sql2)){
                  $num = ++$num;
                  $amount = mysql_num_rows($sql2);
                  $id = $row["id"];
                  $name = $row["name"];
                  $link = $row["link"];

                  if($amount < 0 || $num1 < 0){
                    print '<hr/>
                      <img src="../../img/games/black-ops-2-img.png" />
                      <h2>Black Ops 2: XBOX360</h2>
                      <hr/>
                        <div class="btn-group" style="margin-left: 210px;">
                          <a href="../findamatch.php?g=bl2" class="btn">Find a Match</a>
                          <a href="../reportamatch.php?g=bl2" class="btn">Report a Match</a>
                          <a href="../matchissue.php?g=bl2" class="btn">Match Issue</a>
                          <a href="../support.php?id=bl2" class="btn">Support</a>
                        </div>
                      <hr/>
                      <h2>Featured Teams</h2><hr/>';
                  }else{
                    print '<hr/>
                      <img src="../../img/games/black-ops-2-img.png" />
                      <h2>Black Ops 2: XBOX360</h2>
                      <hr/>
                        <div class="btn-group" style="margin-left: 210px;">
                          <a href="../findamatch.php?g=bl2" class="btn">Find a Match</a>
                          <a href="../reportamatch.php?g=bl2" class="btn">Report a Match</a>
                          <a href="../matchissue.php?g=bl2" class="btn">Match Issue</a>
                          <a href="../support.php?id=bl2" class="btn">Support</a>
                        </div>
                      <hr/>
                      <h2>Featured Teams</h2><hr/>';
                  }
              }
            }

【问题讨论】:

  • 不鼓励将代码放入链接中(因为如果链接停止工作,将来的用户将无法理解您的问题),尤其是对于如此大量的代码(其中大部分与您的问题——尤其是 HTML 和 CSS)。你能提取相关的代码并发布吗?
  • 在检查此人是否在团队中的 if 块中,您似乎没有输出任何内容。只是分配一个值。
  • @DavidRobinson 该操作已更新。
  • @Robbert 我该如何解决这个问题?
  • 您需要检查循环内的 IF 逻辑。您在 while 之前将 $num1 设置为 0,然后永远不要更改它,因此它永远不会小于 0。您继续使用循环内返回的行数(该数字永远不会改变)重新填充 $amount,即行只有通过至少 1 行进入 while 循环才能到达该行,因此金额将始终 >= 1,因此 $amount 永远不会小于 0。您的顶部块将永远不会被击中

标签: php mysql printing echo


【解决方案1】:

为什么不进行这样的简单检查

if(isset($players) && $players < 1){
  $team = "You are not a team";
}else{
.. 
}

【讨论】:

  • 为什么不试试 echo 语句,看看玩家的价值是什么,以及它是否正确出现?
  • 这对我帮助很大。我从来没有想到这一点。谢谢。
猜你喜欢
  • 2021-10-25
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多