S4NDM4N
S4NDM4N

Reputation: 924

How to jump a row in a while loop if condition met

I have created a form which managers can create users for the system. I have a separate table which contains user types Ex: Admin, Manager ... etc.

I use a while loop in my form to drag the above mentioned user roles from the table and draw a set of radio buttons.

My problem is I want to hid the Admin option from the normal manager I used PHP but it only hides the radio button it self not the text next to it my code is below.

Code:

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

I'm thinking of making the while loop skip that 1st line using an IF ... ELSE but I can't get my head around how to do it.

I just want to hide the Admin option.

UPDATE: With the help of mplungjan and Alive to Die I got this problem solved I used the continue method which was more streamline from my point of view now my code looks like this;

Code:

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

Upvotes: 2

Views: 1219

Answers (2)

mplungjan
mplungjan

Reputation: 178385

You can use an if and continue - statements after the continue are ignored

use OR (||) if either uRole==1 or Admin should be skipped

<?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"]; }}?>
}?>

Upvotes: 2

Jagdish Chaudhary
Jagdish Chaudhary

Reputation: 287

You Can Do like This-

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

Upvotes: 1

Related Questions