-2

I am showing a list of users information. The users are set at different levels (1-5). I only want it to display information for users that are level 1. Here is basically the code.

<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
  <tr> 
    <td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
    <td><?php echo $rrows['date']; ?></td>
    <td><?php echo $rrows['user_level']; ?></td>
    <td><?php echo $rrows['user_name'];?></td>
    <td><?php echo $rrows['user_email']; ?></td>
    <td> <span id="approve<?php echo $rrows['id']; ?>"> 
      <?php if(!$rrows['approved']) { echo "Pending"; } else {echo "Active"; }?>
      </span> </td>
    <td><span id="ban<?php echo $rrows['id']; ?>"> 
      <?php if(!$rrows['banned']) { echo "no"; } else {echo "yes"; }?>
      </span> 
    </td>
  </tr>

How would I say to display only information of users who's 'user_level' = '1'?

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
ScudBud
  • 13
  • 3
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 03 '16 at 19:16
  • 1
    `WHERE user_level = 1` or in a `while/if equals to`. C'mon, you can do this. – Funk Forty Niner May 03 '16 at 19:16
  • 1
    Do it in the query as @Fred-ii- says or you can test each row you return with `if(1 == $rrows['user_level']){ // echo the table rows}` – Jay Blanchard May 03 '16 at 19:18
  • 1
    *et voilà!* @JayBlanchard ;-) – Funk Forty Niner May 03 '16 at 19:18
  • 1
    You were right on the money there @JayBlanchard in 3...2...1 Feast!! – Funk Forty Niner May 03 '16 at 19:21
  • I *can* count! LOL @Fred-ii- – Jay Blanchard May 03 '16 at 19:21
  • @JayBlanchard Just like Count Chocula! – Funk Forty Niner May 03 '16 at 19:22

3 Answers3

0

You would need to put an if statement inside your while loop that checks the user level.

//php mysql while 
      If ($rrows['user_level'] == 1) {?>
    //html code
    }?>
0

You could filter in two ways:

  • directly in the query, as indicated by @Fred
  • after fetching the recordset:

while ($rrows = mysql_fetch_array($rs_results)){

      if ($rrows['user_level'] == 1){
         // display as previously
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Webomatik
  • 844
  • 7
  • 7
0

Try put the row to if block, like in the following code. It's ugly practice. Will be better if You use condition in the mysql query.

<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
      <?if($rrows['user_level']==1){?>
      <tr> 
        <td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
        <td><?php echo $rrows['date']; ?></td>
        <td><?php echo $rrows['user_level']; ?></td>
        <td><?php echo $rrows['user_name'];?></td>
        <td><?php echo $rrows['user_email']; ?></td>
        <td> <span id="approve<?php echo $rrows['id']; ?>"> 
          <?php if(!$rrows['approved']) { echo "Pending"; } else {echo "Active"; }?>
          </span> </td>
        <td><span id="ban<?php echo $rrows['id']; ?>"> 
          <?php if(!$rrows['banned']) { echo "no"; } else {echo "yes"; }?>
          </span> </td>
    </tr>
    <?} /*end if*/?>
<?} /*end while*/?>
Dell
  • 930
  • 6
  • 13
  • 1
    Why should the OP "try" this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard May 03 '16 at 19:25