SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using multiple OR statements

    I am using a PHP file to output data to an HTML form, and to make the PHP file easier to maintain I am trying to use 'or' statements (e.g if $test=="1" or "2" or "3" echo...) but every time I do the file outputs data before you actually submit the form. The strange thing is I can use multiple 'or' statements in the first 'if' string but any after that messes it up.

    Hope that makes sense.

    Thanks,
    -Luke

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Show us your code.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Show us your code.
    The whole file is pretty big but here's the beginning part where the problem starts...

    PHP Code:
    <?php
    if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){
    $car=$_POST['carrier'];
    $col=$_POST['color'];
    $sze=$_POST['size'];
    $sof=$_POST['software'];
    $mod=$_POST['model'];
    $con=$_POST['condition'];

    if (
    $car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="Unopened"){
    echo 
    "Your iPhone is worth: $875 - $900";

    }

    }
    if (
    $car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
    echo 
    "Your iPhone is worth: $175 - $190";
    }
    The first part works just fine, but in the second part if I have a third 'or' statement ($sof=="Jailbroken" or "Unlocked" or "Factory") then it outputs the echo before someone actually submits the form. However, if you only have $sof=="Jailbroken" or "Unlocked" then its OK.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sze=="16GB" or "8GB"

    someone correct me if I am wrong, but you can't do this

    you need
    $sze=="16GB" or $sze=="8GB"

    if you don't compare "8GB" to something it will return true and you have this 3 times in your if

    it is like saying
    Code:
    if(1){
       echo "Something";
    else
       echo "Other Thing";
    }
    the else will not execute because it is always true

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would also simplify the ifs

    PHP Code:
    <?php 
    if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){ 
    $car=$_POST['carrier']; 
    $col=$_POST['color']; 
    $sze=$_POST['size']; 
    $sof=$_POST['software']; 
    $mod=$_POST['model']; 
    $con=$_POST['condition']; 

    if (
    $car=="ATT" 
         
    and $col=="Black" 
         
    and ($sze=="16GB" or $sze=="8GB"
         and (
    $sof=="Jailbroken" or $sof=="Unlocked" or $sof=="Factory"
         and 
    $mod=="iPhone 1st Generation" )
    {
      if (
    $con=="Unopened")
      { 
        echo 
    "Your iPhone is worth: $875 - $900"
      }else if (
    $con=="New"){
        echo 
    "Your iPhone is worth: $175 - $190";
      }
    }

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Yeah... think i know what's going on... try using && for and and || for or.

    EDIT : And that giant and at the top wont work. You've gotta check each individual one. (isset($_POST['value1']) && isset($_POST['value2']) etc...)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Shouldn't this kind of thing be checked against a database of values, or at least a csv file?

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Yeah... think i know what's going on... try using && for and and || for or.

    EDIT : And that giant and at the top wont work. You've gotta check each individual one. (isset($_POST['value1']) && isset($_POST['value2']) etc...)
    OK first off I fixed the isset at the top, second I changed all ands to && but when changing the ors to II the form wouldn't even load. However, doing what craqgerbill said and using
    PHP Code:
    if $test=="value1" or $test=="value" 
    so far has fixed the problem.

    I'll report back if I find anything else wrong.
    Thanks

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Shouldn't this kind of thing be checked against a database of values, or at least a csv file?
    I was originally going to use a MySQL database instead of housing all the values in a single PHP file but I don't know practically anything when it comes to SQL =P

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Most of us started the same as you and there are plenty of easy to understand Mysql tutorials out there.

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luxe View Post
    OK first off I fixed the isset at the top, second I changed all ands to && but when changing the ors to II the form wouldn't even load. However, doing what craqgerbill said and using
    PHP Code:
    if $test=="value1" or $test=="value" 
    so far has fixed the problem.

    I'll report back if I find anything else wrong.
    Thanks
    It's not II, it's || (on US keyboards, hold shift and push the \ key).

    Be very careful using 'or'. As has been discussed before, the precedence order of 'or' and 'and' can lead to some unexpected results...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    It's not II, it's || (on US keyboards, hold shift and push the \ key).

    Be very careful using 'or'. As has been discussed before, the precedence order of 'or' and 'and' can lead to some unexpected results...
    I'm on a Mac so it doesn't have that (or maybe it does and I don't know the command). I am using && instead of 'and' now but do you think I'll run into problems because I'm still using 'or'?

  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Well, like is pointed out in that thread... if you ever do something like

    $f = false or true;
    ... $f's value is False.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Luxe View Post
    I'm on a Mac so it doesn't have that (or maybe it does and I don't know the command). I am using && instead of 'and' now but do you think I'll run into problems because I'm still using 'or'?
    So Steve went as far as inventing his own keyboard...
    Well, as a dutch saying goes: who wants to be beautiful has to suffer

    Googling for 'pipeline on mac keyboard' gave me this: http://en.kioskea.net/faq/1593-making-pipe-under-mac
    (the | is called pipeline)

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    &&
    ||

    Just copy and paste those for now then.

  16. #16
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The weird thing is now that I'm using && and || everything in the entire PHP file gets outputted now matter what is selected. But when I use 'and' + || it works properly? Here's a little test file below...

    PHP Code:
    <?php
    if(isset($_POST['carrier']) and isset($_POST['color']) and isset($_POST['size']) and isset($_POST['software']) and isset($_POST['model']) and isset($_POST['condition'])) {
    $car=$_POST['carrier'];
    $col=$_POST['color'];
    $sze=$_POST['size'];
    $sof=$_POST['software'];
    $mod=$_POST['model'];
    $con=$_POST['condition'];

    if (
    $car=="ATT" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 4S" and $con=="Unopened"){
    echo 
    "Your iPhone is worth:";

    }

    }
    if (
    $car=="Sprint" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
    echo 
    "Your iPhone is worth:";
    }

    ?>
    When using 'and' in-place of && and || in place of 'or' everything works properly, but as soon as I change 'and' to && everything in the file will be outputted.

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    When mixing && and || you should put use brackets to avoid any unwanted behaviour:
    PHP Code:
    if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory") && $mod=="iPhone 4S" && $con=="Unopened") {
      echo 
    "Your iPhone is worth:";

    assuming that this is the way you want it to behave

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    When mixing && and || you should put use brackets to avoid any unwanted behaviour
    Ok, I must have screwed something up because nothing works now. I threw together this test file but I can't seem to get it to work! Would anyone mind checking over it because I can't seem to find the problem:

    PHP Code:
    <?php
    if(isset($_POST['carrier']) && isset($_POST['color']) && isset($_POST['size']) && isset($_POST['software']) && isset($_POST['model']) && isset($_POST['condition'])) {
    $car=$_POST['carrier'];
    $col=$_POST['color'];
    $sze=$_POST['size'];
    $sof=$_POST['software'];
    $mod=$_POST['model'];
    $con=$_POST['condition'];

    if (
    $car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory") && $mod=="iPhone 4S" && $con=="Unopened"){
    echo 
    "Your iPhone is worth: output1";

    }

    }
    if (
    $car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || sof=="Unlocked" || sof=="Factory") && $mod=="iPhone 4S" && $con=="Like New"){
    echo 
    "Your iPhone is worth: output2";
    }
    if (
    $car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || sof=="Unlocked" || sof=="Factory") && $mod=="iPhone 4S" && $con=="Good Condition"){
    echo 
    "Your iPhone is worth: output3";
    }



    ?>
    EDIT: I removed the multiple $sof=="" so it now looks like this
    PHP Code:
     if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || "Unlocked" || "Factory") && $mod=="iPhone 4S" && $con=="Good Condition"){
    echo 
    "Your iPhone is worth: output3"; } 
    And everything works properly now, but am I going to run into future problems by not defining each with $sof=="" or does that not matter because of the ()?

  19. #19
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think, where you have,
    }

    }

    you should have only one }
    and that there should be another } at the very end, after the very last }

    PS whenever you think of using more than one AND and/or OR, use ( ) to help control the logic. Replying on the rules of precedence to get things right is always risky, ( ) always works.

  20. #20
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Code:
    ($sof=="Jailbroken" || sof=="Unlocked" || sof=="Factory")
    See what is missing? The same mistake in the last if as well.

  21. #21
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    See what is missing? The same mistake in the last if as well.
    Thanks, can't believe I missed that! Guess I was just looking at it for to long =P


Tags for this Thread

Bookmarks

Posting Permissions

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