php - Making IF ELSE statement with combinations of choice -
currently i'm making application suggest car suits users' preferences based on selection. i'd skip html form choices follow:
type: mpv, suv, sedan
passengers: 5, 7
engine: gasoline, diesel
budget: low, medium, high, luxury
here i'm trying make combination because want every single choice counts. therefore i've done far:
if (isset($_post['submit']) { $type = $_post['type']; $passengers = $_post['passengers']; $engine = $_post['engine']; $budget = $_post['budget']; if ($type == 'mpv' && $passengers == 5 && $engine == 'gasoline' && $budget == 'low') { // execute function here } else if ($type == 'mpv' && $passengers == 7 && $engine == 'gasoline' && $budget == 'low') { // execute function here } else if ($type == 'mpv' && $passengers == 5 && $engine == 'diesel' && $budget == 'low') { // execute function here } // , goes on , on......
i know problem have keep making if/elseif function until list every single possible combination. there better way this? need every single possible combination because there additional calculations done in custom function.
here custom function:
public function calculatedetails($displacement_bottom, $displacement_top, $price_bottom, $price_top, $clearance) { $stmt = $connect->prepare("select id, brand, name cars type = '$type' , displacement between '$displacement_bottom' , '$displacement_top' , price between '$price_bottom' , '$price_top' , clearance = '$clearance' , passengers = '$passengers' , engine = '$engine' limit 3"); $stmt->execute(); while ($row = $stmt->fetch()) { $result = echo htmlspecialchars($row['brand'])." ".htmlspecialchars($row['name']); } return $result; }
so can this
if ($type == 'mpv' && $passengers == 5 && $engine == 'gasoline' && $budget == 'low') { $suggestion = calculatedetails(1500, 2000, 100, 150, 'medium'); echo $suggestion; } else if ($type == 'mpv' && $passengers = 7 && $engine == 'gasoline' && $budget = 'low') { // continue on , on... }
since couldn't find way simplify if/else "loop" (and started thinking giving :( ), came few new functions
public function typedetails($type) { if ($type == 'mpv') { $detailed_type = echo "1500 , 2000"; } else if ($type == 'suv') { $detailed_type = echo "2000 , 3500"; } else { $detailed_type = echo "2000 , 3000"; } return $detailed_type; } public function budgetdetails($budget) { if ($budget == 'low') { $detailed_price = echo "100 , 150"; } else if ($type == 'medium') { $detailed_price = echo "150 , 250"; } else if ($type == 'high') { $detailed_price = echo "250 , 450"; } else { $detailed_price = echo "450 , 800"; } return $detailed_price; } public function groundclearance($type) { if ($type == 'mpv') { $ground_clearance = "medium"; } else if ($type == 'suv') { $ground_clearance = "high"; } else { $ground_clearance = "low"; } return $ground_clearance; }
in hope able instead
$type = $_post['type']; $budget = $_post['budget']; $displacement = typedetails($type); $price = budgetdetails($budget); // same function simplified clause $suggestion = calculatedetails($displacement, $price, $clearance); echo $suggestion;
i'm tired continue today, guys came few more ideas. appreciate replies , comments given here. might not in php, therefore want learn how make things simpler
there not can here. alternative option, may create kind of matrix models using multi level array:
$matrix = [ 'mpv' => [ 'diesel' => [ 5 => [ 'low' => function() { ... }, 'medium' => function() { ... }, ], ], 'gasoline' => [ ... ], ], 'suv' => [ ... ], ]; if (isset($matrix[$type][$engine][$passengers][$budget])) { $matrix[$type][$engine][$passengers][$budget](); }
you may reuse functions if don't differ or set special constants instead of functions passed single custom method. depends on actual functionality have each combination.
Comments
Post a Comment