Jump to content

Best way to add forms with the same data with less code?


alien73

Recommended Posts

I'm building a Cart and have 7 forms all are the same just the form field names differ. Paypal allows up to 7 starting with on0 os0 then on1 os1 then on2 os2 etc.

 

If I add all 7 the code would be huge. Wondering a switch/ case would work?

 

Here's the first option/attribute/price form.

 

// Begin Option/Attributes/Prices 1.

if (!empty($opt1)) {

$output .= '<input type="hidden" name="on0" value="'. $node->ps_product_options1 .'" />';
$output .= '<br /><br />';
$output .= 'Select Option: <strong> '. ucwords($node->ps_product_options1) .'</strong> <br />';
$output .= '<select name="os0">';
$output .= '<option value="Select a '. $node->ps_product_options1 .'">';
$output .= '-- Select a '. ucwords($node->ps_product_options1) .' --</option>';

// Retrieve options/attributes/prices

preg_match_all("/[-|+]\s\d+\.\d{2}/", $attrp1, $matches1);

for($i=0;$i<count($otp1_pieces);$i++) {
  $parts1 = explode(" ", $matches1[0][$i]);

// If attribute prices are left blank add default

  if ($parts1[1] == '') {
  eval ('$get_prices1 = $node->ps_sell_price ' . $parts1[0] . '- 0.00;');
  }
  else {

// Show New Price to pass to Paypal's Cart
  
  if ($node->ps_check_sale_price == 0) {
  eval ('$get_prices1 = $node->ps_sell_price ' . $parts1[0] . ' $parts1[1];');
  }
  else {
  eval ('$get_prices1 = $node->ps_sale_price ' . $parts1[0] . ' $parts1[1];');
  }
  }

  $output .= '<option value="'. ucwords($otp1_pieces[$i]) .'"> '. ucwords($otp1_pieces[$i]) .'';
  $output .= ' '. t('(') .''. curr() .''.number_format($get_prices1, 2) .''. t(')') .' '. showcurrency_abbr() .'</option>';
  }

// Pass Attribute Prices to Paypal
  
  $output .= '<input type="hidden" name="option_index" value="0" />';

  for($i=0;$i<count($otp1_pieces);$i++) { 
   $parts1 = explode(" ", $matches1[0][$i]);
  
  if ($parts1[1] == '') {

// If attribute prices are left blank add default value 
  
  eval ('$get_prices1 = $node->ps_sell_price ' . $parts1[0] . '+ 0.00;');
  }
  else {

// Use Sell or Sale Price
  if ($node->ps_check_sale_price == 0) {
  eval ('$get_prices1 = $node->ps_sell_price ' . $parts1[0] . ' $parts1[1];');
  }
  else {
  eval ('$get_prices1 = $node->ps_sale_price ' . $parts1[0] . ' $parts1[1];');
  }

  }
  $output .= '<input type="hidden" name="option_select'. $i .'" value="'. ucwords($otp1_pieces[$i]) .'" />';
  $output .= '<input type="hidden" name="option_amount'. $i .'" value="'. number_format($get_prices1, 2) .'" />';
  }
  }

// End Option/Attributes/Prices 1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.