| | |
Echo only if two variables are true
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
You have the statement correct but you don't need those extra parenthesis
That should work just fine, I'm not entirely sure what the problem is.
php Syntax (Toggle Plain Text)
if ($Series == "D" && $ModelYear == "2"){ echo "Skylark"; } else { echo "This is not a valid Series"; }
Last edited by ShawnCplus; Jun 5th, 2008 at 4:59 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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";
}
?>
<?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";
}
?>
This is the perfect situation for a switch statement. PHP can use a switch statement on strings unlike C.
php Syntax (Toggle Plain Text)
switch($Series){ case "A": do_something; break; default: case "D": do_something_else; break; }
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
Go take a look at how switch statements work, they aren't if statements.
php Syntax (Toggle Plain Text)
switch($Series){ case 'A': // equivalent to if($Series == 'A') do_something(); break; default: // default handles what happens if none of the cases are true case 'B': // equivalent to else if($Series == 'B') do_something_else(); break; }
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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
I appreciate you help and patience though Shawn!
Thanks,
Arthur
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
just looking but dont you want to set the model year to the value between the H's?
php Syntax (Toggle Plain Text)
$ModelYear=substr($decode,5,1); switch ($ModelYear){ Case 0: break; Case 1: break; Case 2: //Display Skylark echo "Skylark"; break; }
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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?
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- How to Secure and Handling variables (PHP)
- Open In New Window Php (PHP)
- Passing Variables (PHP)
- Inserting dynamic values into Javascript variables (JavaScript / DHTML / AJAX)
- Get error of MSSQl-proc (PHP)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- This ought to be simple - extra spaces (PHP)
Other Threads in the PHP Forum
- Previous Thread: I want to ALLOW line feeds, etc.
- Next Thread: Is there a problem with the or operator in mysql?
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion remote script search seo server sessions sms soap source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






