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

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?

You have the statement correct but you don't need those extra parenthesis

if ($Series == "D" && $ModelYear == "2"){
  echo "Skylark";
} else {
  echo "This is not a valid Series";
}

That should work just fine, I'm not entirely sure what the problem is.

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;


$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.

switch($Series){
case "A":
  do_something;
  break;
default:
case "D":
 do_something_else;
 break;
}

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

Go take a look at how switch statements work, they aren't if statements.

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;
}

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

just looking but dont you want to set the model year to the value between the H's?

$ModelYear=substr($decode,5,1);
switch ($ModelYear){
Case 0: 

break;
Case 1:

break;
Case 2: //Display Skylark
echo "Skylark";
break;
}

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?

cant do

if ($Series=='D'){
switch ($ModelYear){
cases
}
}

?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.