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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where clause, with if statement

    Hi everyone, this is the first time that I've done much with PHP
    My question is how would I include an IF statement within a WHERE clause, so that I can ignore the item if its submitted value is null? $datemonthF and $datedayF are the submitted values.
    Here's what I have now:
    PHP Code:
    $query .= "WHERE datemonth = '"$datemonthF ."' and dateday = '"$datedayF ."'"
    I'd like to negate each one that has the value null. I could also make it so they are negated if the value is nothing, "", whichever is easier. Thanks for any help!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by habester View Post
    Hi everyone, this is the first time that I've done much with PHP
    My question is how would I include an IF statement within a WHERE clause, so that I can ignore the item if its submitted value is null? $datemonthF and $datedayF are the submitted values.
    Here's what I have now:
    PHP Code:
    $query .= "WHERE datemonth = '"$datemonthF ."' and dateday = '"$datedayF ."'"
    I'd like to negate each one that has the value null. I could also make it so they are negated if the value is nothing, "", whichever is easier. Thanks for any help!
    PHP Code:
    $c1 = isset($datemonthF) && !empty($datemonthF);
    $c2 = isset($datedayF) && !empty($datedayF);
    if(
    $c1 || $c2){
      
    $query .= "WHERE";
      if(
    $c1){
         
    $query .= " datemonth = '"$datemonthF ."'";
      }
      if(
    $c1 && $c2){
        
    $query .= " and ";
      }
      if(
    $c2){
        
    $query .= " dateday = '"$datedayF ."'";
      }

    best regards

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perfect! thanks for the help!


  •  

    Posting Permissions

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