943,695 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 646
  • PHP RSS
Jun 7th, 2008
0

Could a switch statement be used here?

Expand Post »
I've got some code using a lot of complex if statements that I'm using for a VIN decoder. I finally got it working and I'm hesitant to alter it but these if statements are huge! If a switch statement could be used to accomplish what the if statements are doing, could someone give me an example using my data? Here's a snippet of code:

php Syntax (Toggle Plain Text)
  1. $ModelYear=substr($decode,5,1);
  2. $AssemblyPlant=strtoupper(substr($decode,6,1));
  3. if ($AssemblyPlant == "2" && ($ModelYear >= 5 && $ModelYear <= 7)){
  4. echo "$AssemblyPlant = St. Therese, Canada<br />";
  5. } elseif ($AssemblyPlant == "7" && $ModelYear == "9"){
  6. echo "$AssemblyPlant = Lordstown, OH<br />";
  7. } elseif ($AssemblyPlant == "B" && $ModelYear == "7"){
  8. echo "$AssemblyPlant = Baltimore, MD<br />";
  9. } elseif ($AssemblyPlant == "C"){
  10. echo "$AssemblyPlant = Southgate, California<br />";
  11. } elseif ($AssemblyPlant == "E" && ($ModelYear >= 6 && $ModelYear <= 9)){
  12. echo "$AssemblyPlant = Linden, NJ<br />";
  13. } elseif ($AssemblyPlant == "G"){
  14. echo "$AssemblyPlant = Framingham, Massachusetts<br />";
  15. } elseif ($AssemblyPlant == "H"){
  16. echo "$AssemblyPlant = Flint, Michigan<br />";
  17. } elseif ($AssemblyPlant == "K" && ($ModelYear >= 5 && $ModelYear <= 7)){
  18. echo "$AssemblyPlant = Leeds, MO<br />";
  19. } elseif ($AssemblyPlant == "L" && ($ModelYear >= 3 && $ModelYear <= 7)){
  20. echo "$AssemblyPlant = Van Nuys, CA<br />";
  21. } elseif ($AssemblyPlant == "N" && $ModelYear == "4"){
  22. echo "$AssemblyPlant = Norwood, OH<br />";
  23. } elseif ($AssemblyPlant == "T" && ($ModelYear >= 6 && $ModelYear <= 9)){
  24. echo "$AssemblyPlant = Tarrytown, NY<br />";
  25. } elseif ($AssemblyPlant == "U" && $ModelYear == "8"){
  26. echo "$AssemblyPlant = Lordstown, OH<br />";
  27. } elseif ($AssemblyPlant == "W" && ($ModelYear >= 8 && $ModelYear <= 9)){
  28. echo "$AssemblyPlant = Willow Run, MI<br />";
  29. } elseif ($AssemblyPlant == "X"){
  30. echo "$AssemblyPlant = Fairfax, Kansas<br />";
  31. } elseif ($AssemblyPlant == "Y" && ($ModelYear >= 2 && $ModelYear <= 5)){
  32. echo "$AssemblyPlant = Wilmington, Delaware<br />";
  33. } elseif ($AssemblyPlant == "Z"){
  34. echo "$AssemblyPlant = Fremont, California<br />";
  35. } else {
  36. echo "Buick's weren't built at this plant in 197$ModelYear. Please check your VIN and try again.<br />";
  37. }

Thanks!
Last edited by peter_budo; Jun 10th, 2008 at 7:47 am. Reason: Keep It Organized - please use [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Jun 8th, 2008
0

Re: Could a switch statement be used here?

Hy, As i read your description I think if you are using switch statements the coding line should be same no difference only thing is that code looking is good but not code line

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
patelamitbe is offline Offline
3 posts
since Jun 2008
Jun 9th, 2008
0

Re: Could a switch statement be used here?

Hi. I don't think a switch statement will make your code better or easier to read.

In my (humble) opoinion you're better off creating a lookup table containing: plant, modelyearfrom, modelyearto, description.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Jun 10th, 2008
0

Re: Could a switch statement be used here?

You can use a switch statament, but you must also use "default" case
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006

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: PHP questions - % of web + finding a developer / host
Next Thread in PHP Forum Timeline: pagination within image - IF problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC