How would i figure out the price difference efficiently with PHP? -


i have page , if scroll second "customize" in middle of page , click see print_r() products , thing trying figure out difference in price between selected , other 2 products....all 3 products in array , if below see radio buttons need....here php looks awful...any suggestions

$current_price = $product[$selected_product]['product_price'];  $standard_price = $product["standard"]['product_price'] - $current_price; $business_price = $product["business"]['product_price']  - $current_price; $premium_price = $product["premium"]['product_price'] - $current_price;  if($standard_price == 0){     $standard_price = "included"; } if($standard_price > 0){     $standard_price = "subtract " . $standard_price; }else{     $standard_price = "add " . $standard_price; }  if($business_price == 0){     $business_price = "included"; } if($business_price > 0){     $business_price = "subtract " . $business_price; }else{     $business_price = "add " . $business_price; }  if($premium_price == 0){     $premium_price = "included"; } if($premium_price > 0){     $premium_price = "subtract " . $premium_price; }else{     $premium_price = "add " . $premium_price; } 

as starter, , seeing repeated code, i'd break comparison out in function. re-use code instead of copy-paste:

function price_delta($base,$compare){   if ($base == $compare)     return 'included';   $delta = $compare - $base;   return $delta > 0 ? sprintf("add $%01.2f", $delta) : sprintf("subtract $%01.2f", $delta * -1); } 

then it's matter of:

$standard_price = price_delta($current_price, $product["standard"]['product_price']); $business_price = price_delta($current_price, $product["business"]['product_price']); $premium_price  = price_delta($current_price, $product["premium"]['product_price']); 

demo


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -