Newb problem: "if" statement returning multiple results as true

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seegoon
    New Member
    • Oct 2009
    • 31

    Newb problem: "if" statement returning multiple results as true

    Hi guys.

    I'm trying to write some php so that, depending on the page displayed, a certain <div> will load a certain class, giving it a certain background image. I have it down in theory, but there's a problem. My current code is returning multiple results as true, leading to the user receiving source like:
    Code:
    <div id="wrap" class="flower-1flower-2">
    In this instance, it is obviously supposed to be either class="flower-1" or class="flower-2".

    Here's my php:

    Code:
    <div id="wrap" class="
    <?php if ($thisPage=="Home"||"Test") echo "flower-1"; 
    if ($thisPage=="Bravo") echo "flower-2"; ?>
    ">
    On the page designated "Bravo", I'm getting the conjoined "flower-1flower-2" problem. I don't know if any of this is clear at all, so I can help explain further if need be.

    Thanks for reading!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You cannot join boolean-operations like that. You must check the values separately.

    Code:
    if ($thisPage == "home" || $thisPage == "test")
    The way you have it, PHP checks the condition $thisPage == "home" then sees the OR operator || and then a string "test". As you should know, a non-zero value in PHP will evaluate to TRUE, so this will always evaluate to true.

    Comment

    • seegoon
      New Member
      • Oct 2009
      • 31

      #3
      Thank you. That has worked a charm and I've learned something I should've known before.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Glad to be of help.

        See ya 'round,
        Mark.

        Comment

        • seegoon
          New Member
          • Oct 2009
          • 31

          #5
          Okay, smartypants. Let's see if there's an easy answer to stage two.

          I'd like a fallback for pages without $thisPage even being on the page. In this instance, I'd like to echo something like "flower-(rand(1, 10))". I know that syntax is way off base, but hopefully what I'm asking for makes sense; that I have 10 "flower" classes and the rand function chooses a number to fill it in.

          This is kind of two things, I realise. And I'm not sure that the first is even possible. But I'm sure the second can be done, right?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Anything is possible! :P

            Something like this:

            Code:
            if ($thisPage == 'home' || $thisPage == 'test') {
                echo 'flower-1';
            } 
            elseif ($thisPage == 'bravo') {
                echo 'flower-2';
            }
            else {
                // A random one
                echo 'flower-', rand(1, 10);
            }
            }

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Hey.

              You can also use switch to accomplish the same.
              [code=php]switch($thisPag e)
              {
              case "home":
              case "test":
              echo "flower-1";
              break;
              case "bravo":
              echo "flower-2";
              break;
              default:
              echo "flower-", mt_rand(1, 10);
              break;
              }[/code]
              Some people prefer this, but I'm more inclined to use the method Markus posted. (This looks to much like goto for my taste.)

              Comment

              • seegoon
                New Member
                • Oct 2009
                • 31

                #8
                Whoa. Thanks guys. That's worked absolutely immaculately, Markus. Alti, I'm curious - what are the advantages to your technique?

                Once this is implemented into the site I'm butchering, I'll PM you a link so you can see your hard work in action!

                Comment

                Working...