User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,935 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,962 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 191 | Replies: 1
Reply
Join Date: Jun 2008
Posts: 17
Reputation: forwardlookguy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Use same part of a variable for two different things

  #1  
Jul 23rd, 2008
I'm trying to decode a variable the user inputs into two different things, depending on the input.

Here it is in context:

User inputs 591P1001; 59 is the year, 1 is the series, P is the assembly plant and 1001 is the CUN.

Now on the same page, if the user inputs 814P1001, the information is decode as 8 is the engine, 1 is the series, 4 is the year, P is the assembly plant and 1001 is the CUN.

How can I use the same variable I've defined as $decode=trim($_POST['VIN']) to display different things based on the input. Right now it works fine for the first example but displays nothing when the second example is input.

Here is my code:
<?php

$decode=trim($_POST['VIN']);

// 1959 - 1963 Pontiac Begins

$ModelYear=substr($decode,1,2);
		if ($ModelYear >= 59 && $ModelYear <= 63){{
					echo "";
}

$Series=substr($decode,0,1);
$ModelYear=substr($decode,1,2);
		if ($Series == "1" && ($ModelYear >= 59 && $ModelYear <= 60)){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "1" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Tempest<br />";
	} elseif ($Series == "2" && $ModelYear == "63"){
					echo "$Series = Lemans<br />";
	} elseif ($Series == "3" && $ModelYear == "60"){
					echo "$Series = Ventura<br />";
	} elseif ($Series == "3" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "4" && ($ModelYear >= 59 && $ModelYear <= 60)){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "5" && $ModelYear == "61"){
					echo "$Series = Ventura<br />";
	} elseif ($Series == "6" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "7" && ($ModelYear >= 59 && $ModelYear <= 62)){
					echo "$Series = Bonneville Safari<br />";
	} elseif ($Series == "8" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$Series = Bonneville<br />";
	} elseif ($Series == "9" && ($ModelYear >= 62 && $ModelYear <= 63)){
					echo "$Series = Grand Prix<br />";
	} else {
					echo "Pontiac didn't offer this series in 19$ModelYear. Please check your VIN and try again.<br />";
}

$ModelYear=substr($decode,1,2);
		if ($ModelYear >= 59 && $ModelYear <= 63){
					echo "$ModelYear = 19$ModelYear<br />";
	} else {
					echo "Invalid model year. Please check your VIN and try again.<br />";
}

$AssemblyPlant=strtoupper(substr($decode,3,1));
		if ($AssemblyPlant == "A" && $ModelYear == "59"){
					echo "$AssemblyPlant = Atlanta, GA<br />";
	} elseif ($AssemblyPlant == "A" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "C" && $ModelYear == "59"){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} elseif ($AssemblyPlant == "D" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Doraville, GA<br />";
	} elseif ($AssemblyPlant == "E" && ($ModelYear >= 60 && $ModelYear <= 61)){
					echo "$AssemblyPlant = Euclid, OH<br />";
	} elseif ($AssemblyPlant == "F" && $ModelYear == "59"){
					echo "$AssemblyPlant = Framingham, MA<br />";
	} elseif ($AssemblyPlant == "K" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Kansas City, KS<br />";
	} elseif ($AssemblyPlant == "L" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Linden, NJ<br />";
	} elseif ($AssemblyPlant == "P" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Pontiac, MI<br />";
	} elseif ($AssemblyPlant == "S" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} elseif ($AssemblyPlant == "T" && $ModelYear == "59"){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "W" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Wilmington, DE<br />";
	} else {
					echo "Pontiac's weren't built at this plant in 19$ModelYear. Please check your VIN and try again.<br />";
}

$ProductionNumber=substr($decode,4,6);
		if (is_Numeric($ProductionNumber)){
					echo "$ProductionNumber = ";
					echo $ProductionNumber - 1001;
					echo "th Pontiac scheduled for production<br />";
	} else {
					echo "Invalid production number. Please check your VIN and try again.<br />";
}

// 1959 - 1963 Pontiac Ends

// 1964 Pontiac Begins

$ModelYear=substr($decode,2,1);
		} elseif ($ModelYear == "4"){{
					echo "";
}
$Engine=substr($decode,0,1);
		if ($Engine == "6"){
					echo "$Engine = 6-cylinder<br />";
	} elseif ($Engine == "8"){
					echo "$Engine = 8-cylinder<br />";
	} else {
					echo "Incorrect engine code. Please check your VIN and try again.<br />";
}

$Series=substr($decode,1,1);
		if ($Series == "0"){
					echo "$Series = Tempest<br />";
	} elseif ($Series == "1"){
					echo "$Series = Tempest Custom<br />";
	} elseif ($Series == "2"){
					echo "$Series = Lemans<br />";
	} elseif ($Series == "3"){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "6"){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "8"){
					echo "$Series = Bonneville<br />";
	} elseif ($Series == "9"){
					echo "$Series = Grand Prix<br />";
	} else {
					echo "Pontiac didn't offer this series in 196$ModelYear. Please check your VIN and try again.<br />";
}

$ModelYear=substr($decode,2,1);
		if ($ModelYear == "4"){
					echo "$ModelYear = 196$ModelYear<br />";
	} else {
					echo "Incorrect model year. Please check your VIN and try again.<br />";
}

$AssemblyPlant=strtoupper(substr($decode,3,1));
		if ($AssemblyPlant == "A"){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "D"){
					echo "$AssemblyPlant = Doraville, GA<br />";
	} elseif ($AssemblyPlant == "F"){
					echo "$AssemblyPlant = Fremont, OH<br />";
	} elseif ($AssemblyPlant == "K"){
					echo "$AssemblyPlant = Kansas City, KS<br />";
	} elseif ($AssemblyPlant == "L"){
					echo "$AssemblyPlant = Linden, NJ<br />";
	} elseif ($AssemblyPlant == "P"){
					echo "$AssemblyPlant = Pontiac, MI<br />";
	} elseif ($AssemblyPlant == "S"){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} else {
					echo "Pontiac's weren't built at this plant in 196$ModelYear. Please check your VIN and try again.<br />";
}

$ProductionNumber=substr($decode,4,6);
		if (is_Numeric($ProductionNumber)){
					echo "$ProductionNumber = ";
					echo $ProductionNumber - 1001;
					echo "th Pontiac scheduled for production<br />";
	} else {
					echo "Invalid production number. Please check your VIN and try again.<br />";
}
}
// 1964 Pontiac Ends
?>

Thanks for the help,
Arthur
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Use same part of a variable for two different things

  #2  
Jul 24th, 2008
Originally Posted by forwardlookguy View Post
I'm trying to decode a variable the user inputs into two different things, depending on the input.

Here it is in context:

User inputs 591P1001; 59 is the year, 1 is the series, P is the assembly plant and 1001 is the CUN.

Now on the same page, if the user inputs 814P1001, the information is decode as 8 is the engine, 1 is the series, 4 is the year, P is the assembly plant and 1001 is the CUN.

How can I use the same variable I've defined as $decode=trim($_POST['VIN']) to display different things based on the input. Right now it works fine for the first example but displays nothing when the second example is input.

Here is my code:
<?php

$decode=trim($_POST['VIN']);

// 1959 - 1963 Pontiac Begins

$ModelYear=substr($decode,1,2);
		if ($ModelYear >= 59 && $ModelYear <= 63){{
					echo "";
}

$Series=substr($decode,0,1);
$ModelYear=substr($decode,1,2);
		if ($Series == "1" && ($ModelYear >= 59 && $ModelYear <= 60)){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "1" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Tempest<br />";
	} elseif ($Series == "2" && $ModelYear == "63"){
					echo "$Series = Lemans<br />";
	} elseif ($Series == "3" && $ModelYear == "60"){
					echo "$Series = Ventura<br />";
	} elseif ($Series == "3" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "4" && ($ModelYear >= 59 && $ModelYear <= 60)){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "5" && $ModelYear == "61"){
					echo "$Series = Ventura<br />";
	} elseif ($Series == "6" && ($ModelYear >= 61 && $ModelYear <= 63)){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "7" && ($ModelYear >= 59 && $ModelYear <= 62)){
					echo "$Series = Bonneville Safari<br />";
	} elseif ($Series == "8" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$Series = Bonneville<br />";
	} elseif ($Series == "9" && ($ModelYear >= 62 && $ModelYear <= 63)){
					echo "$Series = Grand Prix<br />";
	} else {
					echo "Pontiac didn't offer this series in 19$ModelYear. Please check your VIN and try again.<br />";
}

$ModelYear=substr($decode,1,2);
		if ($ModelYear >= 59 && $ModelYear <= 63){
					echo "$ModelYear = 19$ModelYear<br />";
	} else {
					echo "Invalid model year. Please check your VIN and try again.<br />";
}

$AssemblyPlant=strtoupper(substr($decode,3,1));
		if ($AssemblyPlant == "A" && $ModelYear == "59"){
					echo "$AssemblyPlant = Atlanta, GA<br />";
	} elseif ($AssemblyPlant == "A" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "C" && $ModelYear == "59"){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} elseif ($AssemblyPlant == "D" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Doraville, GA<br />";
	} elseif ($AssemblyPlant == "E" && ($ModelYear >= 60 && $ModelYear <= 61)){
					echo "$AssemblyPlant = Euclid, OH<br />";
	} elseif ($AssemblyPlant == "F" && $ModelYear == "59"){
					echo "$AssemblyPlant = Framingham, MA<br />";
	} elseif ($AssemblyPlant == "K" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Kansas City, KS<br />";
	} elseif ($AssemblyPlant == "L" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Linden, NJ<br />";
	} elseif ($AssemblyPlant == "P" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Pontiac, MI<br />";
	} elseif ($AssemblyPlant == "S" && ($ModelYear >= 60 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} elseif ($AssemblyPlant == "T" && $ModelYear == "59"){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "W" && ($ModelYear >= 59 && $ModelYear <= 63)){
					echo "$AssemblyPlant = Wilmington, DE<br />";
	} else {
					echo "Pontiac's weren't built at this plant in 19$ModelYear. Please check your VIN and try again.<br />";
}

$ProductionNumber=substr($decode,4,6);
		if (is_Numeric($ProductionNumber)){
					echo "$ProductionNumber = ";
					echo $ProductionNumber - 1001;
					echo "th Pontiac scheduled for production<br />";
	} else {
					echo "Invalid production number. Please check your VIN and try again.<br />";
}

// 1959 - 1963 Pontiac Ends

// 1964 Pontiac Begins

$ModelYear=substr($decode,2,1);
		} elseif ($ModelYear == "4"){{
					echo "";
}
$Engine=substr($decode,0,1);
		if ($Engine == "6"){
					echo "$Engine = 6-cylinder<br />";
	} elseif ($Engine == "8"){
					echo "$Engine = 8-cylinder<br />";
	} else {
					echo "Incorrect engine code. Please check your VIN and try again.<br />";
}

$Series=substr($decode,1,1);
		if ($Series == "0"){
					echo "$Series = Tempest<br />";
	} elseif ($Series == "1"){
					echo "$Series = Tempest Custom<br />";
	} elseif ($Series == "2"){
					echo "$Series = Lemans<br />";
	} elseif ($Series == "3"){
					echo "$Series = Catalina<br />";
	} elseif ($Series == "6"){
					echo "$Series = Star Chief<br />";
	} elseif ($Series == "8"){
					echo "$Series = Bonneville<br />";
	} elseif ($Series == "9"){
					echo "$Series = Grand Prix<br />";
	} else {
					echo "Pontiac didn't offer this series in 196$ModelYear. Please check your VIN and try again.<br />";
}

$ModelYear=substr($decode,2,1);
		if ($ModelYear == "4"){
					echo "$ModelYear = 196$ModelYear<br />";
	} else {
					echo "Incorrect model year. Please check your VIN and try again.<br />";
}

$AssemblyPlant=strtoupper(substr($decode,3,1));
		if ($AssemblyPlant == "A"){
					echo "$AssemblyPlant = Arlington, TX<br />";
	} elseif ($AssemblyPlant == "D"){
					echo "$AssemblyPlant = Doraville, GA<br />";
	} elseif ($AssemblyPlant == "F"){
					echo "$AssemblyPlant = Fremont, OH<br />";
	} elseif ($AssemblyPlant == "K"){
					echo "$AssemblyPlant = Kansas City, KS<br />";
	} elseif ($AssemblyPlant == "L"){
					echo "$AssemblyPlant = Linden, NJ<br />";
	} elseif ($AssemblyPlant == "P"){
					echo "$AssemblyPlant = Pontiac, MI<br />";
	} elseif ($AssemblyPlant == "S"){
					echo "$AssemblyPlant = Southgate, CA<br />";
	} else {
					echo "Pontiac's weren't built at this plant in 196$ModelYear. Please check your VIN and try again.<br />";
}

$ProductionNumber=substr($decode,4,6);
		if (is_Numeric($ProductionNumber)){
					echo "$ProductionNumber = ";
					echo $ProductionNumber - 1001;
					echo "th Pontiac scheduled for production<br />";
	} else {
					echo "Invalid production number. Please check your VIN and try again.<br />";
}
}
// 1964 Pontiac Ends
?>

Thanks for the help,
Arthur



To make the code a bit more lean, you probably want to look into the "factory method" which is a programming design pattern.

http://en.wikipedia.org/wiki/Factory_method_pattern

The simple factory method in PHP is to use an array instead of a switch or if else in a comparison. (it isn't truly a factory method pattern, but it inherits the concept).

$models = array(
  '59'=>'Catalina', 
  '60'=>'Catalina',
  '61'=>'Tempest',
  '62'=>'Tempest',
  '63'=>'Tempest'
....
);

That way instead of the if else block, you can just the array index:

$model = $models[ substr($decode,1,2) ];

BTW: where is the second example?
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC