943,649 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2634
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 5th, 2008
0

Echo only if two variables are true

Expand Post »
I've hit a new road block on my VIN decoder page. I want to echo something only if two variables are true.

Here's what I want to do:

if (($Series == "D") && ($ModelYear == "2")){
echo "Skylark";
} else {
echo "This is not a valid Series";
}
When I type 4D27H2H111111 into the decoder, it yields the else statement. I have defined the two variables $Series (the D in the above VIN) and $ModelYear (the 2 in the above VIN) and I want to echo "Skylark" only if those two variables are true. For instance, if the VIN was changed to 4D27H4H111111, it would echo the else statement.

Thanks for the help,
Arthur Ash III
Last edited by forwardlookguy; Jun 5th, 2008 at 2:50 pm. Reason: posted before finishing thread
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jun 5th, 2008
0

Re: Echo only if two variables are true

Have you tried echoing out $series and $modelyear in the else statement so you can double check they are being set correctly and its not a problem with the decoder?
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
TommyBs is offline Offline
61 posts
since Mar 2008
Jun 5th, 2008
0

Re: Echo only if two variables are true

You have the statement correct but you don't need those extra parenthesis
php Syntax (Toggle Plain Text)
  1. if ($Series == "D" && $ModelYear == "2"){
  2. echo "Skylark";
  3. } else {
  4. echo "This is not a valid Series";
  5. }
That should work just fine, I'm not entirely sure what the problem is.
Last edited by ShawnCplus; Jun 5th, 2008 at 4:59 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 6th, 2008
0

Re: Echo only if two variables are true

When I echo out that statement in the else statement, nothing shows up. Likewise, when I take away that second set of parenthesis, nothing shows up. In fact, skips over that set of if-else statements altogether and echoes the next set. I'm stumped! Here's my code. I'm going to use switch statements in the future. I'm just trying to get this figured out first.

<?php

$decode=$_POST['VIN'];

$Make=substr($decode,0,1);
if ($Make == "4"){
echo "Buick<br />";
} else {
echo "This is not a valid Make";
}

$Series=strtoupper(substr($decode,1,1));

if ($Series == "D" && $ModelYear == "2"){
echo "Skylark";
} elseif ($Series == "F"){
echo "Sport Wagon";
} elseif ($Series == "G"){
echo "GS";
} elseif ($Series == "H"){
echo "Skylark Custom";
} elseif ($Series == "L"){
echo "Lesabre";
} elseif ($Series == "N"){
echo "Lesabre Custom";
} elseif ($Series == "P"){
echo "Centurion";
} elseif ($Series == "R"){
echo "Estate Wagon";
} elseif ($Series == "U"){
echo "Electra 225";
} elseif ($Series == "V"){
echo "Electra 225 Custom";
} elseif ($Series == "Y"){
echo "Riviera";
} else {
echo "This is not a valid Body Style";
}

$BodyStyle=substr($decode,2,2);
if ($BodyStyle == "27"){
echo "2-door Coupe";
} elseif ($BodyStyle == "35"){
echo "2-seat Station Wagon";
} elseif ($BodyStyle == "36"){
echo "2-seat Sport Wagon";
} elseif ($BodyStyle == "37"){
echo "2-door Hardtop";
} elseif ($BodyStyle == "39"){
echo "4-door Hardtop";
} elseif ($BodyStyle == "45"){
echo "3-seat Station Wagon";
} elseif ($BodyStyle == "47"){
echo "2-door Hardtop";
} elseif ($BodyStyle == "57"){
echo "2-door Hardtop";
} elseif ($BodyStyle == "67"){
echo "2-door Convertible";
} elseif ($BodyStyle == "69"){
echo "4-door Sedan";
} elseif ($BodyStyle == "87"){
echo "2-door Hardtop";
} else {
echo "This is not a valid Body Style";

}

$Engine=strtoupper(substr($decode,4,1));
if ($Engine == "G"){
echo "350 1-2bbl. Dual Exhaust";
} elseif ($Engine == "H"){
echo "350 1-2bbl.";
} elseif ($Engine == "J"){
echo "350 1-4bbl.";
} elseif ($Engine == "K"){
echo "350 1-4bbl. Dual Exhaust";
} elseif ($Engine == "T"){
echo "455 1-4bbl.";
} elseif ($Engine == "U"){
echo "455 1-4bbl. Dual Exhaust";
} elseif ($Engine == "V"){
echo "455 1-4bbl. Stage 1";
} elseif ($Engine == "W"){
echo "455 1-4bbl. Stage 1";
} else {
echo "This is not a valid Engine";
}

$ModelYear=substr($decode,5,1);
if ($ModelYear == "2"){
echo "197$ModelYear";
}else{
echo "This is not a valid Model Year";
}

$AssemblyPlant=strtoupper(substr($decode,6,1));
if ($AssemblyPlant == "C"){
echo "Southgate, California";
} elseif ($AssemblyPlant == "G"){
echo "Framingham, Massachusetts";
} elseif ($AssemblyPlant == "H"){
echo "Flint, Michigan";
} elseif ($AssemblyPlant == "X"){
echo "Fairfax, Kansas";
} elseif ($AssemblyPlant == "Y"){
echo "Wilmington, Delaware";
} elseif ($AssemblyPlant == "Z"){
echo "Fremont, California";
}else{
echo "This is not a valid Assembly Plant";
}

$ProductionNumber=substr($decode,7,6);
if (is_numeric($ProductionNumber)){
echo $ProductionNumber - 100001;
}else {
echo "This is not a valid Production Number";
}

?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jun 6th, 2008
0

Re: Echo only if two variables are true

This is the perfect situation for a switch statement. PHP can use a switch statement on strings unlike C.
php Syntax (Toggle Plain Text)
  1. switch($Series){
  2. case "A":
  3. do_something;
  4. break;
  5. default:
  6. case "D":
  7. do_something_else;
  8. break;
  9. }
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 6th, 2008
0

Re: Echo only if two variables are true

Well, I tried using a switch statement but I'm stuck again. I have this so far:

switch ($Series)
{
case ($Series == "D" && $ModelYear == "2"):
echo "Skylark";
break;

case ($Series == "D" && $ModelYear == "3"):

echo "Century";
default:
echo "This is not a valid Body Style";
break;
}

That's not working. Can you even have something like that for the cases? This is what I'm trying to accomplish: In 1972, D stood for Skylark. In 1973, the D was changed to stand for Century. When the user inputs data, I'm wanting the code to use the model year to determine what the D stands for. Can this be accomplished with a switch statement or even an if statement?

Thanks,
Arthur Ash III
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jun 6th, 2008
0

Re: Echo only if two variables are true

Go take a look at how switch statements work, they aren't if statements.
php Syntax (Toggle Plain Text)
  1. switch($Series){
  2. case 'A': // equivalent to if($Series == 'A')
  3. do_something();
  4. break;
  5. default: // default handles what happens if none of the cases are true
  6. case 'B': // equivalent to else if($Series == 'B')
  7. do_something_else();
  8. break;
  9. }
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 6th, 2008
0

Re: Echo only if two variables are true

I know how they work but the example you posted still isn't doing what I'm trying to accomplish. If a user was to input 4A27H2H111111, then what you posted would echo something for A. Whereas, if the user input 4B27H2H111111 it would echo something for B. I'm pretty sure that's what you are saying. But what I'm trying to do is use the year code (the 2 in between the H's) to determine what the switch statement outputs. That's why I had "case ($Series == "D" && $ModelYear == "2"):" for the first case because I wanted D to echo Skylark if and only if the $ModelYear was "2". It's clear in my head what I want but maybe it's not coming across that way...

I appreciate you help and patience though Shawn!

Thanks,
Arthur
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jun 6th, 2008
0

Re: Echo only if two variables are true

just looking but dont you want to set the model year to the value between the H's?
php Syntax (Toggle Plain Text)
  1. $ModelYear=substr($decode,5,1);
  2. switch ($ModelYear){
  3. Case 0:
  4.  
  5. break;
  6. Case 1:
  7.  
  8. break;
  9. Case 2: //Display Skylark
  10. echo "Skylark";
  11. break;
  12. }
Reputation Points: 31
Solved Threads: 29
Posting Whiz in Training
ProfessorPC is offline Offline
270 posts
since Dec 2007
Jun 6th, 2008
0

Re: Echo only if two variables are true

Thanks for the reply. That wouldn't work for what I'm trying to do either. I only want "Skylark" to display if $Series == "D" && $ModelYear == "2". I'm just looking for a way to incorporate that into my code. I'm not sure if a switch statement or an if statement would accomplish this. From what I've seen, you can't put something like that into a case for a switch statement. Am I wrong? Is there something better to accomplish my goal?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: I want to ALLOW line feeds, etc.
Next Thread in PHP Forum Timeline: Is there a problem with the or operator in mysql?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC