如果满足条件,如何在while循环中跳转一行

时间:2017-05-11 07:39:18

标签: php while-loop

我创建了一个管理员可以为系统创建用户的表单。我有一个单独的表,其中包含用户类型Ex:Admin,Manager ...等。

我在表单中使用while循环从表中拖动上述用户角色并绘制一组单选按钮。

我的问题是我想从我使用PHP的普通管理器中隐藏Admin选项,但它只隐藏单选按钮而不是我的代码所在的旁边的文本。

代码:

<div id="userRoles">
 <label for="userRoles">User Role:</label><br>
  <?php while ($row = $getUserRoleQuery -> fetch(PDO::FETCH_ASSOC)) { ?>
   <input type="radio" class="userRoles" name="userRoles"
    value="<?php echo $row["urId"]; ?>" <?php if ($_SESSION["uRole"] == "1" && $row["userRole"] == "Admin" ){?> hidden <?php } ?>><?php echo $row["userRole"]; }?>
</div>

我想让while循环使用IF ... ELSE跳过第一行,但我无法理解如何做到这一点。

我只是想隐藏Admin选项。

更新:在mplungjan和Alive to Die的帮助下我解决了这个问题我使用了continue方法,从我的观点来看更加流线型,现在我的代码看起来像这样;

代码:

<div id="userRoles">
 <label for="userRoles">User Role:</label><br>
 <?php while ($row = $getUserRoleQuery -> fetch(PDO::FETCH_ASSOC)) {
  if ($_SESSION["uRole"] !== "1" && $row["userRole"] == "Admin" ) continue ?>
  <input type="radio" class="userRoles" name="userRoles" value="<?php echo $row["urId"]; ?>"><?php echo $row["userRole"]; }?>
</div>

2 个答案:

答案 0 :(得分:2)

在忽略继续后,您可以使用if和continue - 语句

如果uRole == 1或者应该跳过管理员

,请使用OR(||)
<?php while ($row = $getUserRoleQuery -> fetch(PDO::FETCH_ASSOC)) { 
   if ($_SESSION["uRole"]=="1" && $row["userRole"] == "Admin") continue; // ignore the rest of the loop
?>
    <input type="radio" class="userRoles" name="userRoles" value="<?php echo $row["urId"]; ?>"><?php echo $row["userRole"]; }}?>
}?>

答案 1 :(得分:1)

你可以这样做 -

<div id="userRoles">
<label for="userRoles">User Role:</label><br>
<?php

 while ($row = $getUserRoleQuery -> fetch(PDO::FETCH_ASSOC))
 {
    if($_SESSION["uRole"] == "1" && $row["userRole"] != "Admin" ))
    {
        echo '<input type="radio" name="userRoles" value="'.$row["urId"].'">'.$row["userRole"].'';
    }
 }
?> 
</div>