Could a switch statement be used here?

Reply

Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Could a switch statement be used here?

 
0
  #1
Jun 7th, 2008
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: patelamitbe is an unknown quantity at this point 
Solved Threads: 0
patelamitbe patelamitbe is offline Offline
Newbie Poster

Re: Could a switch statement be used here?

 
0
  #2
Jun 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 828
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: Could a switch statement be used here?

 
0
  #3
Jun 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 66
Reputation: silviuks is an unknown quantity at this point 
Solved Threads: 11
silviuks silviuks is offline Offline
Junior Poster in Training

Re: Could a switch statement be used here?

 
0
  #4
Jun 10th, 2008
You can use a switch statament, but you must also use "default" case
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC