473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

31 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <div id="wrap" class="flower-1flower-2">
  2.  
In this instance, it is obviously supposed to be either class="flower-1" or class="flower-2".

Here's my php:

Expand|Select|Wrap|Line Numbers
  1. <div id="wrap" class="
  2. <?php if ($thisPage=="Home"||"Test") echo "flower-1"; 
  3. if ($thisPage=="Bravo") echo "flower-2"; ?>
  4. ">
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!
Mar 25 '10 #1
7 3120
Markus
6,050 Recognized Expert Expert
You cannot join boolean-operations like that. You must check the values separately.

Expand|Select|Wrap|Line Numbers
  1. if ($thisPage == "home" || $thisPage == "test")
  2.  
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.
Mar 25 '10 #2
seegoon
31 New Member
Thank you. That has worked a charm and I've learned something I should've known before.
Mar 25 '10 #3
Markus
6,050 Recognized Expert Expert
Glad to be of help.

See ya 'round,
Mark.
Mar 25 '10 #4
seegoon
31 New Member
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?
Mar 25 '10 #5
Markus
6,050 Recognized Expert Expert
Anything is possible! :P

Something like this:

Expand|Select|Wrap|Line Numbers
  1. if ($thisPage == 'home' || $thisPage == 'test') {
  2.     echo 'flower-1';
  3. elseif ($thisPage == 'bravo') {
  4.     echo 'flower-2';
  5. }
  6. else {
  7.     // A random one
  8.     echo 'flower-', rand(1, 10);
  9. }
  10.  
}
Mar 25 '10 #6
Atli
5,058 Recognized Expert Expert
Hey.

You can also use switch to accomplish the same.
Expand|Select|Wrap|Line Numbers
  1. switch($thisPage)
  2. {
  3.     case "home":
  4.     case "test":
  5.         echo "flower-1";
  6.         break;
  7.     case "bravo":
  8.         echo "flower-2";
  9.         break;
  10.     default:
  11.         echo "flower-", mt_rand(1, 10);
  12.         break;
  13. }
Some people prefer this, but I'm more inclined to use the method Markus posted. (This looks to much like goto for my taste.)
Mar 25 '10 #7
seegoon
31 New Member
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!
Mar 26 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
4381
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
1
66842
by: M Wells | last post by:
Hi All, Just wondering if anyone can tell me if you can test for multiple conditions as part of an "IF" statement in T-SQL in SQL Server 2000? ie something like: IF @merr = 1 or @merr=2...
6
8153
by: DLP22192 | last post by:
I have the following single-line if statement that is evaluating true even though it shouldn't. I have never seen this before and I am concerned that this can happen in other areas of my code. ...
6
2254
by: JS | last post by:
In this sample code: if(n==0&&args>1){ for(i=num;args>i+1;i++){ s.length = 0; opt = document.createElement('OPTION'); s.appendChild(opt); opt.value = ""; opt.text = "\74-- Vælg --"; }
10
2666
by: ale.of.ginger | last post by:
Greetings! I am trying to make a multiplayer (no AI, 2 person) game of tic tac toe in Python. So far it has been pretty simple. My only concern is with the win checking to see if a person has...
35
2675
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
6
52911
by: Allerdyce.John | last post by:
Hi, How can I put multiple condition in if statement? I try this, but I can't get that to work. if ( (str != " ") && (str != "") ): Any help is appreciated.
2
2055
by: marsarden | last post by:
write code like: int main(void) { int a=10; if(a<20) {} } Compiler ok on dev-cpp . don't we have to add a ";" after if
9
1454
by: John | last post by:
Hello all. I am a PHP newbie and am having an issue using the && in an if statement. here is the code: if ($_REQUEST == "1" && date("Y-m-d") < $rowWork) { die("<h1>The earlybird special has...
4
2375
by: AKdlm | last post by:
I am using Access 97 (I know) and am creating a lost and found database for materials in our company. I have it all set up so an employee can enter a sales order and pick a specific line/multiple...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.