943,604 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 433
  • PHP RSS
Jul 23rd, 2008
0

Use same part of a variable for two different things

Expand 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 Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $decode=trim($_POST['VIN']);
  4.  
  5. // 1959 - 1963 Pontiac Begins
  6.  
  7. $ModelYear=substr($decode,1,2);
  8. if ($ModelYear >= 59 && $ModelYear <= 63){{
  9. echo "";
  10. }
  11.  
  12. $Series=substr($decode,0,1);
  13. $ModelYear=substr($decode,1,2);
  14. if ($Series == "1" && ($ModelYear >= 59 && $ModelYear <= 60)){
  15. echo "$Series = Catalina<br />";
  16. } elseif ($Series == "1" && ($ModelYear >= 61 && $ModelYear <= 63)){
  17. echo "$Series = Tempest<br />";
  18. } elseif ($Series == "2" && $ModelYear == "63"){
  19. echo "$Series = Lemans<br />";
  20. } elseif ($Series == "3" && $ModelYear == "60"){
  21. echo "$Series = Ventura<br />";
  22. } elseif ($Series == "3" && ($ModelYear >= 61 && $ModelYear <= 63)){
  23. echo "$Series = Catalina<br />";
  24. } elseif ($Series == "4" && ($ModelYear >= 59 && $ModelYear <= 60)){
  25. echo "$Series = Star Chief<br />";
  26. } elseif ($Series == "5" && $ModelYear == "61"){
  27. echo "$Series = Ventura<br />";
  28. } elseif ($Series == "6" && ($ModelYear >= 61 && $ModelYear <= 63)){
  29. echo "$Series = Star Chief<br />";
  30. } elseif ($Series == "7" && ($ModelYear >= 59 && $ModelYear <= 62)){
  31. echo "$Series = Bonneville Safari<br />";
  32. } elseif ($Series == "8" && ($ModelYear >= 59 && $ModelYear <= 63)){
  33. echo "$Series = Bonneville<br />";
  34. } elseif ($Series == "9" && ($ModelYear >= 62 && $ModelYear <= 63)){
  35. echo "$Series = Grand Prix<br />";
  36. } else {
  37. echo "Pontiac didn't offer this series in 19$ModelYear. Please check your VIN and try again.<br />";
  38. }
  39.  
  40. $ModelYear=substr($decode,1,2);
  41. if ($ModelYear >= 59 && $ModelYear <= 63){
  42. echo "$ModelYear = 19$ModelYear<br />";
  43. } else {
  44. echo "Invalid model year. Please check your VIN and try again.<br />";
  45. }
  46.  
  47. $AssemblyPlant=strtoupper(substr($decode,3,1));
  48. if ($AssemblyPlant == "A" && $ModelYear == "59"){
  49. echo "$AssemblyPlant = Atlanta, GA<br />";
  50. } elseif ($AssemblyPlant == "A" && ($ModelYear >= 60 && $ModelYear <= 63)){
  51. echo "$AssemblyPlant = Arlington, TX<br />";
  52. } elseif ($AssemblyPlant == "C" && $ModelYear == "59"){
  53. echo "$AssemblyPlant = Southgate, CA<br />";
  54. } elseif ($AssemblyPlant == "D" && ($ModelYear >= 60 && $ModelYear <= 63)){
  55. echo "$AssemblyPlant = Doraville, GA<br />";
  56. } elseif ($AssemblyPlant == "E" && ($ModelYear >= 60 && $ModelYear <= 61)){
  57. echo "$AssemblyPlant = Euclid, OH<br />";
  58. } elseif ($AssemblyPlant == "F" && $ModelYear == "59"){
  59. echo "$AssemblyPlant = Framingham, MA<br />";
  60. } elseif ($AssemblyPlant == "K" && ($ModelYear >= 59 && $ModelYear <= 63)){
  61. echo "$AssemblyPlant = Kansas City, KS<br />";
  62. } elseif ($AssemblyPlant == "L" && ($ModelYear >= 59 && $ModelYear <= 63)){
  63. echo "$AssemblyPlant = Linden, NJ<br />";
  64. } elseif ($AssemblyPlant == "P" && ($ModelYear >= 59 && $ModelYear <= 63)){
  65. echo "$AssemblyPlant = Pontiac, MI<br />";
  66. } elseif ($AssemblyPlant == "S" && ($ModelYear >= 60 && $ModelYear <= 63)){
  67. echo "$AssemblyPlant = Southgate, CA<br />";
  68. } elseif ($AssemblyPlant == "T" && $ModelYear == "59"){
  69. echo "$AssemblyPlant = Arlington, TX<br />";
  70. } elseif ($AssemblyPlant == "W" && ($ModelYear >= 59 && $ModelYear <= 63)){
  71. echo "$AssemblyPlant = Wilmington, DE<br />";
  72. } else {
  73. echo "Pontiac's weren't built at this plant in 19$ModelYear. Please check your VIN and try again.<br />";
  74. }
  75.  
  76. $ProductionNumber=substr($decode,4,6);
  77. if (is_Numeric($ProductionNumber)){
  78. echo "$ProductionNumber = ";
  79. echo $ProductionNumber - 1001;
  80. echo "th Pontiac scheduled for production<br />";
  81. } else {
  82. echo "Invalid production number. Please check your VIN and try again.<br />";
  83. }
  84.  
  85. // 1959 - 1963 Pontiac Ends
  86.  
  87. // 1964 Pontiac Begins
  88.  
  89. $ModelYear=substr($decode,2,1);
  90. } elseif ($ModelYear == "4"){{
  91. echo "";
  92. }
  93. $Engine=substr($decode,0,1);
  94. if ($Engine == "6"){
  95. echo "$Engine = 6-cylinder<br />";
  96. } elseif ($Engine == "8"){
  97. echo "$Engine = 8-cylinder<br />";
  98. } else {
  99. echo "Incorrect engine code. Please check your VIN and try again.<br />";
  100. }
  101.  
  102. $Series=substr($decode,1,1);
  103. if ($Series == "0"){
  104. echo "$Series = Tempest<br />";
  105. } elseif ($Series == "1"){
  106. echo "$Series = Tempest Custom<br />";
  107. } elseif ($Series == "2"){
  108. echo "$Series = Lemans<br />";
  109. } elseif ($Series == "3"){
  110. echo "$Series = Catalina<br />";
  111. } elseif ($Series == "6"){
  112. echo "$Series = Star Chief<br />";
  113. } elseif ($Series == "8"){
  114. echo "$Series = Bonneville<br />";
  115. } elseif ($Series == "9"){
  116. echo "$Series = Grand Prix<br />";
  117. } else {
  118. echo "Pontiac didn't offer this series in 196$ModelYear. Please check your VIN and try again.<br />";
  119. }
  120.  
  121. $ModelYear=substr($decode,2,1);
  122. if ($ModelYear == "4"){
  123. echo "$ModelYear = 196$ModelYear<br />";
  124. } else {
  125. echo "Incorrect model year. Please check your VIN and try again.<br />";
  126. }
  127.  
  128. $AssemblyPlant=strtoupper(substr($decode,3,1));
  129. if ($AssemblyPlant == "A"){
  130. echo "$AssemblyPlant = Arlington, TX<br />";
  131. } elseif ($AssemblyPlant == "D"){
  132. echo "$AssemblyPlant = Doraville, GA<br />";
  133. } elseif ($AssemblyPlant == "F"){
  134. echo "$AssemblyPlant = Fremont, OH<br />";
  135. } elseif ($AssemblyPlant == "K"){
  136. echo "$AssemblyPlant = Kansas City, KS<br />";
  137. } elseif ($AssemblyPlant == "L"){
  138. echo "$AssemblyPlant = Linden, NJ<br />";
  139. } elseif ($AssemblyPlant == "P"){
  140. echo "$AssemblyPlant = Pontiac, MI<br />";
  141. } elseif ($AssemblyPlant == "S"){
  142. echo "$AssemblyPlant = Southgate, CA<br />";
  143. } else {
  144. echo "Pontiac's weren't built at this plant in 196$ModelYear. Please check your VIN and try again.<br />";
  145. }
  146.  
  147. $ProductionNumber=substr($decode,4,6);
  148. if (is_Numeric($ProductionNumber)){
  149. echo "$ProductionNumber = ";
  150. echo $ProductionNumber - 1001;
  151. echo "th Pontiac scheduled for production<br />";
  152. } else {
  153. echo "Invalid production number. Please check your VIN and try again.<br />";
  154. }
  155. }
  156. // 1964 Pontiac Ends
  157. ?>

Thanks for the help,
Arthur
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jul 24th, 2008
0

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

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)
  1. <?php
  2.  
  3. $decode=trim($_POST['VIN']);
  4.  
  5. // 1959 - 1963 Pontiac Begins
  6.  
  7. $ModelYear=substr($decode,1,2);
  8. if ($ModelYear >= 59 && $ModelYear <= 63){{
  9. echo "";
  10. }
  11.  
  12. $Series=substr($decode,0,1);
  13. $ModelYear=substr($decode,1,2);
  14. if ($Series == "1" && ($ModelYear >= 59 && $ModelYear <= 60)){
  15. echo "$Series = Catalina<br />";
  16. } elseif ($Series == "1" && ($ModelYear >= 61 && $ModelYear <= 63)){
  17. echo "$Series = Tempest<br />";
  18. } elseif ($Series == "2" && $ModelYear == "63"){
  19. echo "$Series = Lemans<br />";
  20. } elseif ($Series == "3" && $ModelYear == "60"){
  21. echo "$Series = Ventura<br />";
  22. } elseif ($Series == "3" && ($ModelYear >= 61 && $ModelYear <= 63)){
  23. echo "$Series = Catalina<br />";
  24. } elseif ($Series == "4" && ($ModelYear >= 59 && $ModelYear <= 60)){
  25. echo "$Series = Star Chief<br />";
  26. } elseif ($Series == "5" && $ModelYear == "61"){
  27. echo "$Series = Ventura<br />";
  28. } elseif ($Series == "6" && ($ModelYear >= 61 && $ModelYear <= 63)){
  29. echo "$Series = Star Chief<br />";
  30. } elseif ($Series == "7" && ($ModelYear >= 59 && $ModelYear <= 62)){
  31. echo "$Series = Bonneville Safari<br />";
  32. } elseif ($Series == "8" && ($ModelYear >= 59 && $ModelYear <= 63)){
  33. echo "$Series = Bonneville<br />";
  34. } elseif ($Series == "9" && ($ModelYear >= 62 && $ModelYear <= 63)){
  35. echo "$Series = Grand Prix<br />";
  36. } else {
  37. echo "Pontiac didn't offer this series in 19$ModelYear. Please check your VIN and try again.<br />";
  38. }
  39.  
  40. $ModelYear=substr($decode,1,2);
  41. if ($ModelYear >= 59 && $ModelYear <= 63){
  42. echo "$ModelYear = 19$ModelYear<br />";
  43. } else {
  44. echo "Invalid model year. Please check your VIN and try again.<br />";
  45. }
  46.  
  47. $AssemblyPlant=strtoupper(substr($decode,3,1));
  48. if ($AssemblyPlant == "A" && $ModelYear == "59"){
  49. echo "$AssemblyPlant = Atlanta, GA<br />";
  50. } elseif ($AssemblyPlant == "A" && ($ModelYear >= 60 && $ModelYear <= 63)){
  51. echo "$AssemblyPlant = Arlington, TX<br />";
  52. } elseif ($AssemblyPlant == "C" && $ModelYear == "59"){
  53. echo "$AssemblyPlant = Southgate, CA<br />";
  54. } elseif ($AssemblyPlant == "D" && ($ModelYear >= 60 && $ModelYear <= 63)){
  55. echo "$AssemblyPlant = Doraville, GA<br />";
  56. } elseif ($AssemblyPlant == "E" && ($ModelYear >= 60 && $ModelYear <= 61)){
  57. echo "$AssemblyPlant = Euclid, OH<br />";
  58. } elseif ($AssemblyPlant == "F" && $ModelYear == "59"){
  59. echo "$AssemblyPlant = Framingham, MA<br />";
  60. } elseif ($AssemblyPlant == "K" && ($ModelYear >= 59 && $ModelYear <= 63)){
  61. echo "$AssemblyPlant = Kansas City, KS<br />";
  62. } elseif ($AssemblyPlant == "L" && ($ModelYear >= 59 && $ModelYear <= 63)){
  63. echo "$AssemblyPlant = Linden, NJ<br />";
  64. } elseif ($AssemblyPlant == "P" && ($ModelYear >= 59 && $ModelYear <= 63)){
  65. echo "$AssemblyPlant = Pontiac, MI<br />";
  66. } elseif ($AssemblyPlant == "S" && ($ModelYear >= 60 && $ModelYear <= 63)){
  67. echo "$AssemblyPlant = Southgate, CA<br />";
  68. } elseif ($AssemblyPlant == "T" && $ModelYear == "59"){
  69. echo "$AssemblyPlant = Arlington, TX<br />";
  70. } elseif ($AssemblyPlant == "W" && ($ModelYear >= 59 && $ModelYear <= 63)){
  71. echo "$AssemblyPlant = Wilmington, DE<br />";
  72. } else {
  73. echo "Pontiac's weren't built at this plant in 19$ModelYear. Please check your VIN and try again.<br />";
  74. }
  75.  
  76. $ProductionNumber=substr($decode,4,6);
  77. if (is_Numeric($ProductionNumber)){
  78. echo "$ProductionNumber = ";
  79. echo $ProductionNumber - 1001;
  80. echo "th Pontiac scheduled for production<br />";
  81. } else {
  82. echo "Invalid production number. Please check your VIN and try again.<br />";
  83. }
  84.  
  85. // 1959 - 1963 Pontiac Ends
  86.  
  87. // 1964 Pontiac Begins
  88.  
  89. $ModelYear=substr($decode,2,1);
  90. } elseif ($ModelYear == "4"){{
  91. echo "";
  92. }
  93. $Engine=substr($decode,0,1);
  94. if ($Engine == "6"){
  95. echo "$Engine = 6-cylinder<br />";
  96. } elseif ($Engine == "8"){
  97. echo "$Engine = 8-cylinder<br />";
  98. } else {
  99. echo "Incorrect engine code. Please check your VIN and try again.<br />";
  100. }
  101.  
  102. $Series=substr($decode,1,1);
  103. if ($Series == "0"){
  104. echo "$Series = Tempest<br />";
  105. } elseif ($Series == "1"){
  106. echo "$Series = Tempest Custom<br />";
  107. } elseif ($Series == "2"){
  108. echo "$Series = Lemans<br />";
  109. } elseif ($Series == "3"){
  110. echo "$Series = Catalina<br />";
  111. } elseif ($Series == "6"){
  112. echo "$Series = Star Chief<br />";
  113. } elseif ($Series == "8"){
  114. echo "$Series = Bonneville<br />";
  115. } elseif ($Series == "9"){
  116. echo "$Series = Grand Prix<br />";
  117. } else {
  118. echo "Pontiac didn't offer this series in 196$ModelYear. Please check your VIN and try again.<br />";
  119. }
  120.  
  121. $ModelYear=substr($decode,2,1);
  122. if ($ModelYear == "4"){
  123. echo "$ModelYear = 196$ModelYear<br />";
  124. } else {
  125. echo "Incorrect model year. Please check your VIN and try again.<br />";
  126. }
  127.  
  128. $AssemblyPlant=strtoupper(substr($decode,3,1));
  129. if ($AssemblyPlant == "A"){
  130. echo "$AssemblyPlant = Arlington, TX<br />";
  131. } elseif ($AssemblyPlant == "D"){
  132. echo "$AssemblyPlant = Doraville, GA<br />";
  133. } elseif ($AssemblyPlant == "F"){
  134. echo "$AssemblyPlant = Fremont, OH<br />";
  135. } elseif ($AssemblyPlant == "K"){
  136. echo "$AssemblyPlant = Kansas City, KS<br />";
  137. } elseif ($AssemblyPlant == "L"){
  138. echo "$AssemblyPlant = Linden, NJ<br />";
  139. } elseif ($AssemblyPlant == "P"){
  140. echo "$AssemblyPlant = Pontiac, MI<br />";
  141. } elseif ($AssemblyPlant == "S"){
  142. echo "$AssemblyPlant = Southgate, CA<br />";
  143. } else {
  144. echo "Pontiac's weren't built at this plant in 196$ModelYear. Please check your VIN and try again.<br />";
  145. }
  146.  
  147. $ProductionNumber=substr($decode,4,6);
  148. if (is_Numeric($ProductionNumber)){
  149. echo "$ProductionNumber = ";
  150. echo $ProductionNumber - 1001;
  151. echo "th Pontiac scheduled for production<br />";
  152. } else {
  153. echo "Invalid production number. Please check your VIN and try again.<br />";
  154. }
  155. }
  156. // 1964 Pontiac Ends
  157. ?>

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)
  1. $models = array(
  2. '59'=>'Catalina',
  3. '60'=>'Catalina',
  4. '61'=>'Tempest',
  5. '62'=>'Tempest',
  6. '63'=>'Tempest'
  7. ....
  8. );

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

PHP Syntax (Toggle Plain Text)
  1. $model = $models[ substr($decode,1,2) ];

BTW: where is the second example?
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

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: Python and PHP integration help: Incorrect Image URL decode?
Next Thread in PHP Forum Timeline: intializing variable at time object creation





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


Follow us on Twitter


© 2011 DaniWeb® LLC