DBookatay Posted November 2, 2010 Share Posted November 2, 2010 I have a "switch statement" that is a bit ridiculous switch($row['warr_Ext_Term']) { case '24 month / 24,000 miles': $month24 = date('Y-m-d',strtotime($date_sold . '+730 days')); $warr_Ext_Exp_Date = substr($month24, 5, 2).'-'.substr($month24, 8, 2).'-'.substr($month24, 2, 2); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 24000).' miles'; $today = date('Y-m-d'); if ($month24 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; } $warr_Ext_Cmpy = ' <li><label>Extended</label>'.$row['warr_Ext_Cmpy'].'</li> <li><label>Term</label>'.$row['warr_Ext_Term'].'</li> <li><label>Contract #</label>'.$row['warr_Ext_Num'].'</li> <li><label>'.$warr_Ext_Exp_Text.'</label><span class="'.$class.'">'.$warr_Ext_Exp_Date.'</span><br><label> </label><span class="'.$class.'">'.$warr_Ext_Exp_Miles.'</span></li>' ; What it does is determines when a customers warranty expires, or when it did expire based on the miles that they bought the vehicle at, and based on today's date. The problem is that I sell numerous warranties from 9 days, 4,500 miles up to 120 months, 100,000 miles. Is there a better way to write this, based on the info pulled from the "$row['warr_Ext_Term']" row? Any help would be greatly appreciated... Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/ Share on other sites More sharing options...
revraz Posted November 2, 2010 Share Posted November 2, 2010 I assume you are using a database for this. If so, why not use a ID for each warranty period, ie 1 = 9 days/4500 miles. Inside that row, also store the days and miles, and you can use that column data as your variable to do computations against. That should allow you to remove the entire Switch block. Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129283 Share on other sites More sharing options...
DBookatay Posted November 2, 2010 Author Share Posted November 2, 2010 I assume you are using a database for this. If so, why not use a ID for each warranty period, ie 1 = 9 days/4500 miles. Inside that row, also store the days and miles, and you can use that column data as your variable to do computations against. That should allow you to remove the entire Switch block. Because the database is spanning back 7 years worth of information. Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129465 Share on other sites More sharing options...
DBookatay Posted November 2, 2010 Author Share Posted November 2, 2010 This is the entire code, with all the different options. There has to be a better way! switch($row['warr_Ext_Term']) { case '90 days / 4,500 miles': $day90 = date('Y-m-d',strtotime($date_sold . '+90 days')); $warr_Ext_Exp_Date = substr($day90, 5, 2).'-'.substr($day90, 8, 2).'-'.substr($day90, 2, 2); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 4500).' miles'; $today = date('Y-m-d'); if ($day90 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '180 days / 7,500 miles': $day180 = date('Y-m-d',strtotime($date_sold . '+180 days')); $warr_Ext_Exp_Date = substr($day180, 5, 2).'-'.substr($day180, 8, 2).'-'.substr($day180, 2, 2); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 7500).' miles'; $today = date('Y-m-d'); if ($day180 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '6 month / 6,000 miles': $month6 = date('Y-m-d',strtotime($date_sold . '+6 months')); $warr_Ext_Exp_Date = substr($month6, 5, 2).'-'.substr($month6, 8, 2).'-'.substr($month6, 2, 2); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 6000).' miles'; $today = date('Y-m-d'); if ($month6 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '12 month / 12,000 miles': $month12 = date('Y-m-d',strtotime($date_sold . '+12 months')); $warr_Ext_Exp_Date = substr($month12, 5, 2).'-'.substr($month12, 8, 2).'-'.substr($month12, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 12000).' miles'; $today = date('Y-m-d'); if ($month12 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '12 month / 14,500 miles': $month12 = date('Y-m-d',strtotime($date_sold . '+12 months')); $warr_Ext_Exp_Date = substr($month12, 5, 2).'-'.substr($month12, 8, 2).'-'.substr($month12, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 14500).' miles'; $today = date('Y-m-d'); if ($month12 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '12 month / 15,000 miles': $month12 = date('Y-m-d',strtotime($date_sold . '+12 months')); $warr_Ext_Exp_Date = substr($month12, 5, 2).'-'.substr($month12, 8, 2).'-'.substr($month12, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 15000).' miles'; $today = date('Y-m-d'); if ($month12 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '13 month / 13,000 miles': $month13 = date('Y-m-d',strtotime($date_sold . '+13 months')); $warr_Ext_Exp_Date = substr($month13, 5, 2).'-'.substr($month13, 8, 2).'-'.substr($month13, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 13000).' miles'; $today = date('Y-m-d'); if ($month13 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '14 month / 14,000 miles': $month14 = date('Y-m-d',strtotime($date_sold . '+14 months')); $warr_Ext_Exp_Date = substr($month14, 5, 2).'-'.substr($month14, 8, 2).'-'.substr($month14, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 14000).' miles'; $today = date('Y-m-d'); if ($month14 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '15 month / 15,000 miles': $month15 = date('Y-m-d',strtotime($date_sold . '+15 months')); $warr_Ext_Exp_Date = substr($month15, 5, 2).'-'.substr($month15, 8, 2).'-'.substr($month15, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 15000).' miles'; $today = date('Y-m-d'); if ($month15 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '16 month / 16,000 miles': $month16 = date('Y-m-d',strtotime($date_sold . '+16 months')); $warr_Ext_Exp_Date = substr($month16, 5, 2).'-'.substr($month16, 8, 2).'-'.substr($month16, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 16000).' miles'; $today = date('Y-m-d'); if ($month16 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '17 month / 17,000 miles': $month17 = date('Y-m-d',strtotime($date_sold . '+17 months')); $warr_Ext_Exp_Date = substr($month17, 5, 2).'-'.substr($month17, 8, 2).'-'.substr($month17, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 17000).' miles'; $today = date('Y-m-d'); if ($month17 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '18 month / 18,000 miles': $month18 = date('Y-m-d',strtotime($date_sold . '+18 months')); $warr_Ext_Exp_Date = substr($month18, 5, 2).'-'.substr($month18, 8, 2).'-'.substr($month18, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 18000).' miles'; $today = date('Y-m-d'); if ($month18 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '19 month / 19,000 miles': $month19 = date('Y-m-d',strtotime($date_sold . '+19 months')); $warr_Ext_Exp_Date = substr($month19, 5, 2).'-'.substr($month19, 8, 2).'-'.substr($month19, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 19000).' miles'; $today = date('Y-m-d'); if ($month19 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '20 month / 20,000 miles': $month20 = date('Y-m-d',strtotime($date_sold . '+20 months')); $warr_Ext_Exp_Date = substr($month20, 5, 2).'-'.substr($month20, 8, 2).'-'.substr($month20, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 20000).' miles'; $today = date('Y-m-d'); if ($month20 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '21 month / 21,000 miles': $month21 = date('Y-m-d',strtotime($date_sold . '+21 months')); $warr_Ext_Exp_Date = substr($month21, 5, 2).'-'.substr($month21, 8, 2).'-'.substr($month21, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 21000).' miles'; $today = date('Y-m-d'); if ($month21 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '22 month / 22,000 miles': $month22 = date('Y-m-d',strtotime($date_sold . '+22 months')); $warr_Ext_Exp_Date = substr($month22, 5, 2).'-'.substr($month22, 8, 2).'-'.substr($month22, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 22000).' miles'; $today = date('Y-m-d'); if ($month22 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '23 month / 23,000 miles': $month23 = date('Y-m-d',strtotime($date_sold . '+23 months')); $warr_Ext_Exp_Date = substr($month23, 5, 2).'-'.substr($month23, 8, 2).'-'.substr($month23, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 23000).' miles'; $today = date('Y-m-d'); if ($month23 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '24 month / 24,000 miles': $month24 = date('Y-m-d',strtotime($date_sold . '+24 months')); $warr_Ext_Exp_Date = substr($month24, 5, 2).'-'.substr($month24, 8, 2).'-'.substr($month24, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 24000).' miles'; $today = date('Y-m-d'); if ($month24 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '24 month / 26,500 miles': $month24 = date('Y-m-d',strtotime($date_sold . '+24 months')); $warr_Ext_Exp_Date = substr($month24, 5, 2).'-'.substr($month24, 8, 2).'-'.substr($month24, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 26500).' miles'; $today = date('Y-m-d'); if ($month24 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '24 month / 30,000 miles': $month24 = date('Y-m-d',strtotime($date_sold . '+24 months')); $warr_Ext_Exp_Date = substr($month24, 5, 2).'-'.substr($month24, 8, 2).'-'.substr($month24, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 30000).' miles'; $today = date('Y-m-d'); if ($month24 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '25 month / 25,000 miles': $month25 = date('Y-m-d',strtotime($date_sold . '+25 months')); $warr_Ext_Exp_Date = substr($month25, 5, 2).'-'.substr($month25, 8, 2).'-'.substr($month25, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 25000).' miles'; $today = date('Y-m-d'); if ($month25 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '26 month / 26,000 miles': $month26 = date('Y-m-d',strtotime($date_sold . '+26 months')); $warr_Ext_Exp_Date = substr($month26, 5, 2).'-'.substr($month26, 8, 2).'-'.substr($month26, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 26000).' miles'; $today = date('Y-m-d'); if ($month26 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '27 month / 27,000 miles': $month27 = date('Y-m-d',strtotime($date_sold . '+27 months')); $warr_Ext_Exp_Date = substr($month27, 5, 2).'-'.substr($month27, 8, 2).'-'.substr($month27, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 27000).' miles'; $today = date('Y-m-d'); if ($month27 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '28 month / 28,000 miles': $month28 = date('Y-m-d',strtotime($date_sold . '+28 months')); $warr_Ext_Exp_Date = substr($month28, 5, 2).'-'.substr($month28, 8, 2).'-'.substr($month28, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 28000).' miles'; $today = date('Y-m-d'); if ($month28 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '29 month / 29,000 miles': $month29 = date('Y-m-d',strtotime($date_sold . '+29 months')); $warr_Ext_Exp_Date = substr($month29, 5, 2).'-'.substr($month29, 8, 2).'-'.substr($month29, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 29000).' miles'; $today = date('Y-m-d'); if ($month29 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '30 month / 30,000 miles': $month30 = date('Y-m-d',strtotime($date_sold . '+30 months')); $warr_Ext_Exp_Date = substr($month30, 5, 2).'-'.substr($month30, 8, 2).'-'.substr($month30, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 30000).' miles'; $today = date('Y-m-d'); if ($month30 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '31 month / 31,000 miles': $month31 = date('Y-m-d',strtotime($date_sold . '+31 months')); $warr_Ext_Exp_Date = substr($month31, 5, 2).'-'.substr($month31, 8, 2).'-'.substr($month31, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 31000).' miles'; $today = date('Y-m-d'); if ($month31 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '32 month / 32,000 miles': $month32 = date('Y-m-d',strtotime($date_sold . '+32 months')); $warr_Ext_Exp_Date = substr($month32, 5, 2).'-'.substr($month32, 8, 2).'-'.substr($month32, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 32000).' miles'; $today = date('Y-m-d'); if ($month32 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '33 month / 33,000 miles': $month33 = date('Y-m-d',strtotime($date_sold . '+33 months')); $warr_Ext_Exp_Date = substr($month33, 5, 2).'-'.substr($month33, 8, 2).'-'.substr($month33, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 33000).' miles'; $today = date('Y-m-d'); if ($month33 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '34 month / 34,000 miles': $month34 = date('Y-m-d',strtotime($date_sold . '+34 months')); $warr_Ext_Exp_Date = substr($month34, 5, 2).'-'.substr($month34, 8, 2).'-'.substr($month34, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 34000).' miles'; $today = date('Y-m-d'); if ($month34 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '35 month / 35,000 miles': $month35 = date('Y-m-d',strtotime($date_sold . '+35 months')); $warr_Ext_Exp_Date = substr($month35, 5, 2).'-'.substr($month35, 8, 2).'-'.substr($month35, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 35000).' miles'; $today = date('Y-m-d'); if ($month35 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '36 month / 36,000 miles': $month36 = date('Y-m-d',strtotime($date_sold . '+36 months')); $warr_Ext_Exp_Date = substr($month36, 5, 2).'-'.substr($month36, 8, 2).'-'.substr($month36, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 36000).' miles'; $today = date('Y-m-d'); if ($month36 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '36 month / 38,500 miles': $month36 = date('Y-m-d',strtotime($date_sold . '+36 months')); $warr_Ext_Exp_Date = substr($month36, 5, 2).'-'.substr($month36, 8, 2).'-'.substr($month36, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 38500).' miles'; $today = date('Y-m-d'); if ($month36 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '36 month / 45,000 miles': $month36 = date('Y-m-d',strtotime($date_sold . '+36 months')); $warr_Ext_Exp_Date = substr($month36, 5, 2).'-'.substr($month36, 8, 2).'-'.substr($month36, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 45000).' miles'; $today = date('Y-m-d'); if ($month36 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '37 month / 37,000 miles': $month37 = date('Y-m-d',strtotime($date_sold . '+37 months')); $warr_Ext_Exp_Date = substr($month37, 5, 2).'-'.substr($month37, 8, 2).'-'.substr($month37, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 37000).' miles'; $today = date('Y-m-d'); if ($month37 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '38 month / 38,000 miles': $month38 = date('Y-m-d',strtotime($date_sold . '+38 months')); $warr_Ext_Exp_Date = substr($month38, 5, 2).'-'.substr($month38, 8, 2).'-'.substr($month38, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 38000).' miles'; $today = date('Y-m-d'); if ($month38 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '39 month / 39,000 miles': $month39 = date('Y-m-d',strtotime($date_sold . '+39 months')); $warr_Ext_Exp_Date = substr($month39, 5, 2).'-'.substr($month39, 8, 2).'-'.substr($month39, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 39000).' miles'; $today = date('Y-m-d'); if ($month39 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '40 month / 40,000 miles': $month40 = date('Y-m-d',strtotime($date_sold . '+40 months')); $warr_Ext_Exp_Date = substr($month40, 5, 2).'-'.substr($month40, 8, 2).'-'.substr($month40, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 40000).' miles'; $today = date('Y-m-d'); if ($month40 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '41 month / 41,000 miles': $month41 = date('Y-m-d',strtotime($date_sold . '+41 months')); $warr_Ext_Exp_Date = substr($month41, 5, 2).'-'.substr($month41, 8, 2).'-'.substr($month41, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 41000).' miles'; $today = date('Y-m-d'); if ($month48 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '42 month / 42,000 miles': $month42 = date('Y-m-d',strtotime($date_sold . '+42 months')); $warr_Ext_Exp_Date = substr($month42, 5, 2).'-'.substr($month42, 8, 2).'-'.substr($month42, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 42000).' miles'; $today = date('Y-m-d'); if ($month48 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '43 month / 43,000 miles': $month43 = date('Y-m-d',strtotime($date_sold . '+43 months')); $warr_Ext_Exp_Date = substr($month43, 5, 2).'-'.substr($month43, 8, 2).'-'.substr($month43, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 43000).' miles'; $today = date('Y-m-d'); if ($month43 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '44 month / 44,000 miles': $month44 = date('Y-m-d',strtotime($date_sold . '+44 months')); $warr_Ext_Exp_Date = substr($month44, 5, 2).'-'.substr($month44, 8, 2).'-'.substr($month44, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 44000).' miles'; $today = date('Y-m-d'); if ($month44 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '45 month / 45,000 miles': $month45 = date('Y-m-d',strtotime($date_sold . '+45 months')); $warr_Ext_Exp_Date = substr($month45, 5, 2).'-'.substr($month45, 8, 2).'-'.substr($month45, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 45000).' miles'; $today = date('Y-m-d'); if ($month45 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '46 month / 46,000 miles': $month46 = date('Y-m-d',strtotime($date_sold . '+46 months')); $warr_Ext_Exp_Date = substr($month46, 5, 2).'-'.substr($month46, 8, 2).'-'.substr($month46, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 46000).' miles'; $today = date('Y-m-d'); if ($month46 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '47 month / 47,000 miles': $month47 = date('Y-m-d',strtotime($date_sold . '+47 months')); $warr_Ext_Exp_Date = substr($month47, 5, 2).'-'.substr($month47, 8, 2).'-'.substr($month47, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 47000).' miles'; $today = date('Y-m-d'); if ($month47 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '48 month / 48,000 miles': $month48 = date('Y-m-d',strtotime($date_sold . '+48 months')); $warr_Ext_Exp_Date = substr($month48, 5, 2).'-'.substr($month48, 8, 2).'-'.substr($month48, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 48000).' miles'; $today = date('Y-m-d'); if ($month48 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '48 month / 50,000 miles': $month48 = date('Y-m-d',strtotime($date_sold . '+48 months')); $warr_Ext_Exp_Date = substr($month48, 5, 2).'-'.substr($month48, 8, 2).'-'.substr($month48, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 50000).' miles'; $today = date('Y-m-d'); if ($month48 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '48 month / 75,000 miles': $month48 = date('Y-m-d',strtotime($date_sold . '+48 months')); $warr_Ext_Exp_Date = substr($month48, 5, 2).'-'.substr($month48, 8, 2).'-'.substr($month48, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 75000).' miles'; $today = date('Y-m-d'); if ($month48 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '49 month / 49,000 miles': $month49 = date('Y-m-d',strtotime($date_sold . '+49 months')); $warr_Ext_Exp_Date = substr($month49, 5, 2).'-'.substr($month49, 8, 2).'-'.substr($month49, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 49000).' miles'; $today = date('Y-m-d'); if ($month49 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '50 month / 50,000 miles': $month50 = date('Y-m-d',strtotime($date_sold . '+50 months')); $warr_Ext_Exp_Date = substr($month50, 5, 2).'-'.substr($month50, 8, 2).'-'.substr($month50, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 50000).' miles'; $today = date('Y-m-d'); if ($month50 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '51 month / 51,000 miles': $month51 = date('Y-m-d',strtotime($date_sold . '+51 months')); $warr_Ext_Exp_Date = substr($month51, 5, 2).'-'.substr($month51, 8, 2).'-'.substr($month51, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 51000).' miles'; $today = date('Y-m-d'); if ($month51 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '52 month / 52,000 miles': $month52 = date('Y-m-d',strtotime($date_sold . '+52 months')); $warr_Ext_Exp_Date = substr($month52, 5, 2).'-'.substr($month52, 8, 2).'-'.substr($month52, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 52000).' miles'; $today = date('Y-m-d'); if ($month52 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '53 month / 53,000 miles': $month53 = date('Y-m-d',strtotime($date_sold . '+53 months')); $warr_Ext_Exp_Date = substr($month53, 5, 2).'-'.substr($month53, 8, 2).'-'.substr($month53, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 53000).' miles'; $today = date('Y-m-d'); if ($month51 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '54 month / 54,000 miles': $month54 = date('Y-m-d',strtotime($date_sold . '+54 months')); $warr_Ext_Exp_Date = substr($month54, 5, 2).'-'.substr($month54, 8, 2).'-'.substr($month54, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 54000).' miles'; $today = date('Y-m-d'); if ($month54 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '55 month / 55,000 miles': $month55 = date('Y-m-d',strtotime($date_sold . '+55 months')); $warr_Ext_Exp_Date = substr($month55, 5, 2).'-'.substr($month55, 8, 2).'-'.substr($month55, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 55000).' miles'; $today = date('Y-m-d'); if ($month55 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '56 month / 56,000 miles': $month56 = date('Y-m-d',strtotime($date_sold . '+56 months')); $warr_Ext_Exp_Date = substr($month56, 5, 2).'-'.substr($month56, 8, 2).'-'.substr($month56, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 56000).' miles'; $today = date('Y-m-d'); if ($month56 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '57 month / 57,000 miles': $month57 = date('Y-m-d',strtotime($date_sold . '+57 months')); $warr_Ext_Exp_Date = substr($month57, 5, 2).'-'.substr($month57, 8, 2).'-'.substr($month57, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 57000).' miles'; $today = date('Y-m-d'); if ($month57 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '60 month / 60,000 miles': $month60 = date('Y-m-d',strtotime($date_sold . '+60 months')); $warr_Ext_Exp_Date = substr($month60, 5, 2).'-'.substr($month60, 8, 2).'-'.substr($month60, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 60000).' miles'; $today = date('Y-m-d'); if ($month60 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '60 month / 64,000 miles': $month60 = date('Y-m-d',strtotime($date_sold . '+60 months')); $warr_Ext_Exp_Date = substr($month60, 5, 2).'-'.substr($month60, 8, 2).'-'.substr($month60, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 64000).' miles'; $today = date('Y-m-d'); if ($month60 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '60 month / 70,000 miles': $month60 = date('Y-m-d',strtotime($date_sold . '+60 months')); $warr_Ext_Exp_Date = substr($month60, 5, 2).'-'.substr($month60, 8, 2).'-'.substr($month60, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 70000).' miles'; $today = date('Y-m-d'); if ($month60 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '60 month / 100,000 miles': $month60 = date('Y-m-d',strtotime($date_sold . '+60 months')); $warr_Ext_Exp_Date = substr($month60, 5, 2).'-'.substr($month60, 8, 2).'-'.substr($month60, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 100000).' miles'; $today = date('Y-m-d'); if ($month60 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '72 month / 70,000 miles': $month72 = date('Y-m-d',strtotime($date_sold . '+72 months')); $warr_Ext_Exp_Date = substr($month72, 5, 2).'-'.substr($month72, 8, 2).'-'.substr($month72, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 70000).' miles'; $today = date('Y-m-d'); if ($month72 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '72 month / 90,000 miles': $month72 = date('Y-m-d',strtotime($date_sold . '+72 months')); $warr_Ext_Exp_Date = substr($month72, 5, 2).'-'.substr($month72, 8, 2).'-'.substr($month72, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 90000).' miles'; $today = date('Y-m-d'); if ($month72 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '84 month / 100,000 miles': $month84 = date('Y-m-d',strtotime($date_sold . '+84 months')); $warr_Ext_Exp_Date = substr($month84, 5, 2).'-'.substr($month84, 8, 2).'-'.substr($month84, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 100000).' miles'; $today = date('Y-m-d'); if ($month84 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; case '120 month / 100,000 miles': $month120 = date('Y-m-d',strtotime($date_sold . '+120 months')); $warr_Ext_Exp_Date = substr($month120, 5, 2).'-'.substr($month120, 8, 2).'-'.substr($month120, 0, 4); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 100000).' miles'; $today = date('Y-m-d'); if ($month120 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; } Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129544 Share on other sites More sharing options...
kenrbnsn Posted November 2, 2010 Share Posted November 2, 2010 There's definitely a better way. You are doing almost the exact same code in each option. This is an obvious application for a function. Try something like this: <?php function check_warr($term,$date_sold,$mileage) { list($len_text,$miles_text) = explode(' / ',$term); list($len,$type) = explode(' ',$len_text); list($miles) = explode(' ',$miles_text); $miles = str_replace(',',$miles); // get rid of comma $type = ($type == 'month')?'months':$type; $warr_date = strtotime($date_sold . '+' . $len . ' ' . $type); $warr_ext = array(date('m-d-Y',$warr_date),number_format($mileage + $miles) . ' miles'); $warr_text = ($warr_date > time())?'Expires':'<span class="red">Expired</span>'; $warr_ext[] = $warr_text; return ($warr_ext); } list($warr_Ext_Exp_Date, $warr_Ext_Exp_Miles, $warr_Ext_Exp_Text) = check_warr($row['warr_Ext_Term'],$date_sold,$row['mileage']); ?> Note: not tested, may contain syntax errors. Ken Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129569 Share on other sites More sharing options...
DBookatay Posted November 2, 2010 Author Share Posted November 2, 2010 There's definitely a better way. You are doing almost the exact same code in each option. This is an obvious application for a function. Try something like this: <?php function check_warr($term,$date_sold,$mileage) { list($len_text,$miles_text) = explode(' / ',$term); list($len,$type) = explode(' ',$len_text); list($miles) = explode(' ',$miles_text); $miles = str_replace(',',$miles); // get rid of comma $type = ($type == 'month')?'months':$type; $warr_date = strtotime($date_sold . '+' . $len . ' ' . $type); $warr_ext = array(date('m-d-Y',$warr_date),number_format($mileage + $miles) . ' miles'); $warr_text = ($warr_date > time())?'Expires':'<span class="red">Expired</span>'; $warr_ext[] = $warr_text; return ($warr_ext); } list($warr_Ext_Exp_Date, $warr_Ext_Exp_Miles, $warr_Ext_Exp_Text) = check_warr($row['warr_Ext_Term'],$date_sold,$row['mileage']); ?> Note: not tested, may contain syntax errors. Ken WOW! This works MUCH better! Thank you very much!!! There is one error: "Wrong parameter count for str_replace() on line 225" Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129575 Share on other sites More sharing options...
kenrbnsn Posted November 2, 2010 Share Posted November 2, 2010 That line should be <?php $miles = str_replace(',','',$miles); // get rid of comma ?> Ken Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129588 Share on other sites More sharing options...
DBookatay Posted November 2, 2010 Author Share Posted November 2, 2010 Worked like a charm, thank you! Link to comment https://forums.phpfreaks.com/topic/217509-help-with-a-switch-statement/#findComment-1129595 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.