| | |
Use same part of a variable for two different things
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'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:
Thanks for the help,
Arthur
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 Syntax (Toggle Plain Text)
<?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
•
•
•
•
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 Syntax (Toggle Plain Text)
<?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).
PHP Syntax (Toggle Plain Text)
$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:
PHP Syntax (Toggle Plain Text)
$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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- 'Object variable or With block variable not set' Error (ASP.NET)
- Help with Inventory part 6 program please!! (Java)
- Finding the lowest of five test scores (C++)
- ambiguous part 3 (C)
- Newbie: get part of filename path (JavaScript / DHTML / AJAX)
- GUI buttons Inventory Part 5 (Java)
- Perl/CGI (Saving Data) Part III (Computer Science)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
- Graphics In Pixel: Part II (C++)
Other Threads in the PHP Forum
- Previous Thread: Python and PHP integration help: Incorrect Image URL decode?
- Next Thread: intializing variable at time object creation
| Thread Tools | Search this Thread |
advanced alerts apache api archive array autosuggest beginner binary broken cakephp checkbox class clients cms code cron curl database date datepart display dynamic echo email emptydisplayvalue eregi error execute explodefunction file files folder form forms function functions google hack head href htaccess html if...loop image include insert ip javasciptvalidation javascript joomla keywords library limit link login mail matching menu mlm multiple mysql number object oop password paypal pdf php phpincludeissue query radio random recursive remote script search searchbox server sessions shot smarty source space speed sql syntax system table tutorial update upload url validator variable vbulletin video web website youtube






