Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    While loop, works for 1, not for AND

    So I am trying to get a while loop to display all staff members on a script. Administrators are level 1, and Secondary staff is level 3. That's the only two I want to include. Now if I set it for level 1 staff, it works. Or if I set it for level 3 staff, it works. but the AND will not work, nothing will be displayed that way. I don't understand...

    Code:
    $staffquery = doquery("SELECT user,stafflevel FROM {{table}} WHERE stafflevel='1' AND stafflevel='3' ORDER BY stafflevel", "users");
    
    while ($staffrow = mysql_fetch_array($staffquery)) {
    
        if ($staffrow["stafflevel"] == 1) { $person = "<font color=red><b>Administrator</b></font>"; }
    
        elseif ($staffrow["stafflevel"] == 3) { $person = "<font color=green><b>Help Aid</b></font>"; }
        
        $page .= "<tr><td>".$staffrow["user"]."</td><td>$person</td></tr>";
    
    }

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,803
    Thanks
    3
    Thanked 443 Times in 433 Posts
    There are no rows where stafflevel='1' AND stafflevel='3' at the same time. To get all the rows where stafflevel='1' OR stafflevel='3', you need to use OR.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Ah, so AND would be picking them together for 1 output, or OR is for multiple outputs.....correct?

  4. #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    AND is the same as &&
    OR is the same as ||

    AND means both statements must be true.
    OR means either statements can be true.

  5. #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,511
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You could also use IN, which is similar to PHP's in_array() function:
    Code:
    SELECT user,stafflevel FROM {{table}} WHERE stafflevel IN ( 1, 3 ) ORDER BY stafflevel


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •